发布时间:2019-08-13 07:41:17编辑:auto阅读(1409)
# coding:utf-8
import pymysql
# 打开数据库连接
db = pymysql.connect(host='localhost', port=3306,
user='username', passwd='password', db='database_name', charset='utf8')
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 插入语句
sql = "INSERT INTO EMPLOYEE(FIRST_NAME, AGE, SEX) VALUES (%s,%s,%s)"
# 一个tuple或者list
T = (('xiaoming', 31, 'boy'), ('hong', 22, 'girl'), ('wang', 90, 'man'))
try:
# 执行sql语句
cursor.executemany(sql, T)
# 提交到数据库执行
db.commit()
except :
# 如果发生错误则回滚
db.rollback()
# 关闭游标
cursor.close()
# 关闭数据库连接
db.close()
转自:https://www.ctolib.com/TracyMcgrady6-pymsql_Operation.html
上一篇: python3 json数据包含中文的读
下一篇: python检测端口是否被侦听
46587
44489
35920
33485
28171
24741
23661
18867
18161
16846
4656°
5215°
4745°
4878°
5543°
4506°
4559°
5099°
5003°
6252°