Pymysql实现往表中插入数据过程解析
chuyaoxin 人气:1代码如下
# -*- coding = utf-8 -*- # @time:2020/5/28/028 21:00 # Author:cyx # @File:插入数据.py # @Software:PyCharm import pymysql con = pymysql.connect(host='localhost',user='root',password='123456',database='python_db',port=3366) # 创建游标对象 cur = con.cursor() # 编写插入数据的sql sql = 'insert into t_student(sname,age,score) values(%s,%s,%s)' try: # 执行sql cur.executemany(sql,[('小楚',21,99.9),('小刘',20,59),('小朱',20,85),('小宋',22,95)]) # 提交事务 con.commit() print('插入成功') except Exception as e: print(e) con.rollback() print('插入失败') finally: con.close()
结果
然后到Navicat for MySQL中验证
加载全部内容