forked from JayChen123/textCluster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
33 lines (25 loc) · 719 Bytes
/
db.py
File metadata and controls
33 lines (25 loc) · 719 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
# -*- coding:utf-8 -*-
import MySQLdb
class MyDB(object):
"""
封装管理数据库的上下文
"""
def __init__(self, db_info):
assert isinstance(db_info, dict)
self.conn = MySQLdb.connect(**db_info)
self.cursor = self.conn.cursor()
def __enter__(self):
return self.cursor
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is None:
self.conn.commit()
self.cursor.close()
self.conn.close()
return True
if exc_val:
self.conn.rollback()
self.cursor.close()
self.conn.close()
print(exc_tb)
print(exc_type)
return True