-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
48 lines (48 loc) · 870 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
#encoding=utf-8
con,c=None,None
_dbpath=r".\menu.db"
def connect(dbpath=_dbpath):
global con,c
import sqlite3
con=sqlite3.connect(dbpath)
c=con.cursor()
return
def close():
global con
if con != None:
con.close()
c=None
def getattrs(dbname):
connect()
try:
c.execute('select * from [%s]'%dbname)
return [x[0] for x in c.description]
except Exception, e:
return []
close()
class DbObject(object):
def __init__(self):
self._dic={}
def set(self,key,val):
self._dic[key]=val
setattr(self,key,val)
def as_dic(self):
return self._dic
def query(cmd):
connect()
res=[]
try:
c.execute(cmd)
raw=c.fetchall()
props=[x[0] for x in c.description]
for item in raw:
o=DbObject()
for i in range(len(props)):
o.set(props[i],item[i])
res.append(o)
except Exception, e:
return e
return res
def commit():
global con
con.commit()