发布时间:2019-08-13 07:41:17编辑:auto阅读(1753)
# 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检测端口是否被侦听
49963
49218
39877
36916
31323
28161
27112
21912
21844
20187
117°
179°
166°
322°
347°
956°
1744°
1660°
1344°
2734°