-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_template.py
47 lines (34 loc) · 1.17 KB
/
db_template.py
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
41
42
43
44
45
46
47
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-04-15 16:07:27
# @copyright by hoojo@2018
# @changelog Added python3 `db -> template` example
import pymysql
import contextlib
#定义上下文管理器,连接后自动关闭连接
@contextlib.contextmanager
def mysql_template(host="localhost", user="root", password="root", db="test", port=3306, charset='utf8'):
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="root", db="test", port=3306, charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
# 游标设置为字典类型
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
try:
yield cursor
db.commit()
except OSError as e:
print('error: %s' % e)
finally:
cursor.close()
# 关闭数据库连接
db.close()
# 使用模板查询
with mysql_template() as cursor:
print(cursor)
row_count = cursor.execute("select * from EMPLOYEE")
row = cursor.fetchone()
print(row_count)
print(row)