Python 访问mysql
又来,python访问mysql超简单示例!自从用上了python,就深深的爱上了python,虽然性能不咋地,但是好用啊!
Python代码
import pymysql
# 连接
client = pymysql.connect(host='127.0.0.1',port=3306,user='test',password='123456',database='student', charset='utf8',connect_timeout=10)
# 获取游标
cursor = client.cursor(pymysql.cursors.DictCursor)
# sql查询
sql = "select name, score from math where class = '12';"
rows = cursor.execute(sql)
data = cursor.fetchall()
# 打印结果
print(data)
要点
- 最好封装一下,这仅仅是个样例。
- 一般功能性满足,性能要求可以自测一下。
总结
pymysql具体接口可以参看site-packages/pymysql。欢迎联系博主一起学习讨论。