问题1:数据库链接
pip install pymysql
import pymysql.cursors
# 连接数据库
connect = pymysql.Connect(
host='localhost',
port=3310,
user='woider',
passwd='3243',
db='python',
charset='utf8'
)
# 获取游标
cursor = connect.cursor()
def insert(nickname, image):
# 插入数据
sql = "INSERT INTO user (nickname, image) VALUES ( '%s', '%s' )"
# data = ('雷军', '13512345678')
data = (nickname, image)
cursor.execute(sql % data)
connect.commit()
print('成功插入', cursor.rowcount, '条数据')
def select(nickname):
# 查询数据
sql = "SELECT nickname,image FROM user WHERE nickname = '%s' "
data = (nickname)
cursor.execute(sql % nickname)
for row in cursor.fetchall():
print("Name:%s\tSaving:%s" % row)
print('共查找出', cursor.rowcount, '条数据')
if cursor.rowcount > 0:
return True
else:
return False