发布时间:2019-08-13 07:41:17编辑:auto阅读(1903)
# 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检测端口是否被侦听
51945
51737
42034
38876
33365
30333
28975
23990
23905
22264
432°
2610°
3297°
2745°
2725°
3486°
2693°
3521°
5806°
5595°