-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_insert_format.py
More file actions
37 lines (28 loc) · 900 Bytes
/
db_insert_format.py
File metadata and controls
37 lines (28 loc) · 900 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
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-04-15 14:19:29
# @copyright by hoojo@2018
# @changelog Added python3 `db -> db insert format` 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', '%d', '%s', '%d')" % ('alex', 'alex chen', 21, 'M', 567777)
print(sql)
try:
# 执行插入语句
cursor.execute(sql)
# 提交操作
db.commit()
print('提交')
except OSError as e:
print('error: %s' % e)
# 异常就回滚
db.rollback()
print('回滚')
# 关闭数据库连接
db.close()