-
Notifications
You must be signed in to change notification settings - Fork 2
/
DB.py
73 lines (72 loc) · 2.02 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sqlite3
class DB:
def __init__(self,db=None, busy_timeout=600000):
self.tbl = None
self.dbh = None
self.db = db
def set_db(self, db):
if db:
self.db = db
else:
return 'EDB'
def connect(self):
if not self.db:
return 'EDB'
self.dbh = sqlite3.connect(self.db)
def disconnect(self):
if self.dbh:
self.dbh.close()
def do_query(self, sql):
if not self.dbh or not sql:
return 'EDB'
try:
self.dbh.execute(sql)
self.dbh.commit()
except:
return 'EDB'
def select_query(self, sql):
if not self.dbh or not sql:
return 'EDB'
try:
return self.dbh.execute(sql).fetchall()
except:
return 'EDB'
def insert(self,args):
if(args['tbl']):
self.tbl = args['tbl']
if not self.dbh or not self.tbl or not args['data']:
return 'EDB'
sql = 'insert into ' + self.tbl + ' (' + ','.join(args['data'].keys()) + ') values ('
for x in args['data'].values():
sql += "'"+ x +"'"
sql += ')'
try:
self.dbh.execute(sql)
self.dbh.commit()
return 0
except:
return 'EDB'
def update(self, args):
if(args['tbl']):
self.tbl = args['tbl']
if not self.dbh or not self.tbl or not args['data']:
return 'EDB'
sql = 'update ' + self.tbl + ' set '
x = []
while 1:
try:
y = args['data'].popitem()
x.append(y[0]+"='"+y[1]+"'")
except:
break
sql += ','.join(x)
args['condition'] = args['condition'].lstrip().rstrip()
if args['condition']:
sql += 'where '+ args['condition']
print sql
try:
self.dbh.execute(sql)
self.dbh.commit()
return 0
except:
return 'EDB'