-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_insert_many.py
More file actions
40 lines (31 loc) · 965 Bytes
/
db_insert_many.py
File metadata and controls
40 lines (31 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-04-15 14:11:56
# @copyright by hoojo@2018
# @changelog Added python3 `db -> db insert many` example
import pymysql
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="root", db="test", port=3306, charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
sql = '''
insert into EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)
values(%s, %s, %s, %s, %s)
'''
try:
# 执行插入语句
row = cursor.executemany(sql, [('tom2', 'tom Li', 31, 'W', 21000), ('bili2', 'alex dk', 31, 'W', 21000)])
print('row: ', row)
# 提交操作
db.commit()
print('提交')
except OSError as e:
print('error: %s' % e)
# 异常就回滚
db.rollback()
print('回滚')
# 关闭数据库连接
db.close()