1 # 引入pymysql模块 2 import pymysql as pm 3 4 # 数据库连接 5 db = pm.connect(host='localhost', user='root', password="123456",database='task', charset='utf8') 6 # 建立游标 7 cur = db.cursor() 8 # 设置sql语句 9 sql = 'SELECT * FROM video_info WHERE actor_name=%s' 10 # 执行sql语句 11 cur.execute(sql,'刘德华') 12 # 保存符合条件结果集 13 results = cur.fetchall() 14 # 打印结果集 15 print(results) 16 # 关闭游标和断开数据库连接 17 cur.close() 18 db.close()
测试结果截图: