1.连接
import pymysql # 创建连接 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456",db="test") # 创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 effect_row = cursor.execute("select * from class") print(effect_row) print(cursor.fetchone()) # 打印一条数据 print(cursor.fetchall()) # 打印所有,接着以前读过位置 # 关闭游标 cursor.close() # 关闭连接 conn.close()
2.执行插入命令
import pymysql # 创建连接 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456",db="test") # 创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 effect_row = cursor.executemany("insert into class(id,name)values(%s,%s)", [(8,"liu"),(9,"sun")]) # 提交,不然无法保存新建或者修改的数据 conn.commit() # 关闭游标 cursor.close() # 关闭连接 conn.close()
标签:游标,pymysql,cursor,模块,mysql,close,conn,row From: https://www.cnblogs.com/circlecircle/p/18508562