计算机网络/计算机科学与应用/系统/运维/开发

Python【基础】数据库操作


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

泰山崩于前而色不变,麋鹿兴于左而目不瞬,然后可以制利害,可以待敌。--心术

评论

^