Python【基础】数据库操作
Python •
•
566次阅读
import pymysql
# 链接数据库
con = pymysql.connect(host='localhost', user='root', password='123456', database='mytest', charset='utf8')
# 创建游标
cur = con.cursor()
# 生成数据库
sql = 'select * from mytable'
# 获取结果
cur.execute(sql)
# 获取所有记录 fetchall 获取所有记录 fetchmany 获取多条记录,需传参 fetchone 获取一条记录
dall = cur.fetchall()
# 输出查询结果
print(dall)
# 关闭游标
cur.close()
# 关闭数据库连接,释放内存
cur.close()
# 打印结果
((1, 'zhangs', 19),)
Python
好的习惯,排名第一的是:自律;排名第二的是:终身学习;排名第三的是:保持运动。拥有这三种习惯,美好人生垂手可得。