-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
51 lines (44 loc) · 1.01 KB
/
db.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
48
49
50
51
import logging
import pymysql
import re
import subprocess
# Change this if necessary
CONFIG = {
'DB': 'guest',
'USER': 'guest',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': 3306,
'CHARSET': 'utf8'
}
global DB
DB = None
def connect():
global DB
c = pymysql.connect(db=CONFIG['DB'],
user=CONFIG['USER'],
password=CONFIG['PASSWORD'],
host=CONFIG['HOST'],
port=CONFIG['PORT'],
charset=CONFIG['CHARSET'],
cursorclass=pymysql.cursors.DictCursor)
c._cursor_ = c.cursor()
DB = c
logging.info('Connected to database %s' % CONFIG['DB'])
def execute(sql,args=None):
global DB
sql = re.sub('\s+',' ', sql)
print(sql)
logging.info('SQL: {} Args: {}'.format(sql,args))
DB._cursor_.execute(sql, args)
return DB._cursor_
def commit():
global DB
DB.commit()
def rollback():
global DB
DB.rollback()
def close():
global DB
DB._cursor_.close()
DB.close()