-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
51 lines (37 loc) · 789 Bytes
/
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 pymysql
import config
def getdb():
return pymysql.connect(host=config.SQL_ADDR, user=config.SQL_USER, password=config.SQL_PASSWORD, db=config.SQL_DATABASE)
db = getdb()
def execute(s, args=None, tdb=None):
if tdb is None:
tdb = db
try:
cur = tdb.cursor()
cur.execute(s, args)
except pymysql.err.IntegrityError:
pass
return cur
def select(s, args=None, tdb=None):
if tdb is None:
tdb = db
cur = tdb.cursor()
cur.execute(s, args)
return cur
def select_first(s, args=None, tdb=None):
if tdb is None:
tdb = db
for i in select(s, args, tdb):
return i
return None
def count(s, args=None, tdb=None):
if tdb is None:
tdb = db
cur = tdb.cursor()
cur.execute(s, args)
for i in cur:
return i[0]
def renew():
global db
db.close()
db = getdb()