pymysql模块
- 这是一个python操作mysql的一个模块
- 链接数据库
- 创建游标 (用于执行sql语句)
- 执行sql语句
- 获取结果集
- 关闭链接
import pymysql
1连接数据库
db = pymysql.connect(host="127.0.0.1",user="root",passwd="你指望我把密码打出来吗",port=3306,db='test_sql',charset="utf8")
2创建游标对象 (负责执行sql的工具man)
cursor = db.cursor()
3 准备sql语句
sql="select * from tb_student limit 10"
4调用游标执行
cursor.execute(sql)
5获取结果集
res = cursor.fetcall()
for i in res:
print (i)
cursor.close()释放句柄对象
db.close()关闭链接