代码如下
代码语言:javascript复制# -*- 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中验证
以上就是本文的全部内容,希望对大家的学习有所帮助。