-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqltool.py
More file actions
34 lines (27 loc) · 851 Bytes
/
sqltool.py
File metadata and controls
34 lines (27 loc) · 851 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
#!/usr/bin/env python
# coding=utf-8
import MySQLdb
from server import cfg
class SqlTool(object):
"""Tool to deal with SQL"""
def __init__(self):
self.connect = MySQLdb.connect(
host=cfg.get('mysql', 'host'),
port=cfg.getint('mysql', 'port'),
user=cfg.get('mysql', 'user'),
passwd=cfg.get('mysql', 'pwd'),
db=cfg.get('mysql', 'db'),
charset='utf8',
)
self.cursor = self.connect.cursor()
def __enter__(self):
"""back to 'with SqlTool()' """
return self.cursor
def __exit__(self, exc_type, exc_instance, traceback):
"""end and clean the result"""
if exc_instance:
print exc_instance
self.cursor.close()
self.connect.commit()
self.connect.close()
return True