-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
131 lines (102 loc) · 3.63 KB
/
Copy pathdb.py
File metadata and controls
131 lines (102 loc) · 3.63 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
#coding: utf-8
import os, sys
import sqlite3, locale, uuid
import types
import config
createtable=['create table if not exists guildinfo (guildname varchar(64),gene_num integer not null default 0)',
'create table if not exists members (id integer primary key autoincrement, masterid integer not null, nickname varchar(64) not null, realname varchar(64) not null,sex bool default 0, generation integer not null)',
'create table if not exists verinfo(version varchar(32), sys varchar(32))',
]
class DB():
def __init__(self, path):
self.localcharset = locale.getdefaultlocale()[1]
self.charset = 'utf-8'
self.path = path
if type(path) == types.UnicodeType:
self.path = path.encode(self.charset)
self.db = sqlite3.connect(self.path)
self.version = ''
self.init()
def init(self):
for s in createtable:
self.execute(s)
sql = "select * from verinfo"
ret = self.query(sql, True)
if not ret:
isql = "insert into verinfo(version, sys) values (?,?)"
self.execute_param(isql, (config.VERSION, sys.platform,))
self.version = config.VERSION
else:
self.version = ret[0]['version']
isql = "update verinfo set version='%s'" % (config.VERSION)
self.execute(sql)
def close(self):
self.db.close()
self.db = None
def execute(self, sql, autocommit=True):
self.db.execute(sql)
if autocommit:
self.commit()
def commit(self):
self.db.commit()
def query(self,sql,iszip=True):
if type(sql) == types.UnicodeType:
sql = sql.encode(self.charset, 'ignore')
cur = self.db.cursor()
cur.execute(sql)
res = cur.fetchall()
ret = []
if res and iszip:
des = cur.description
names = [x[0] for x in des]
for line in res:
ret.append(dict(zip(names, line)))
else:
ret = res
cur.close()
return ret
def query_one(self, sql):
if type(sql) == types.UnicodeType:
sql = sql.encode(self.charset, 'ignore')
cur = self.db.cursor()
cur.execute(sql)
one = cur.fetchone()
cur.close()
ret = []
if one:
des = cur.description
names = [x[0] for x in des]
ret.append(dict(zip(names, res)))
return ret
def query_many(self,sql,size=1,iszip=True):
if type(sql) == types.UnicodeType:
sql = sql.encode(self.charset, 'ignore')
cur = self.db.cursor()
cur.execute(sql)
res = cur.fetchmany(size)
ret = []
if res and iszip:
des = cur.description
names = [x[0] for x in des]
for line in res:
ret.append(dict(zip(names, line)))
else:
ret = res
cur.close()
return ret
def rollback(self):
self.db.rollback()
def execute_param(self, sql, param, autocommit=True):
self.db.execute(sql, param)
if autocommit:
self.db.commit()
def test():
db = DB('test.db')
db.execute('create table if not exists testme (id integer primary key autoincrement, name varchar(256))')
db.execute("insert into testme(name) values ('Lambert')")
print db.query("select * from testme")
print db.query("select * from verinfo")
db.close()
if __name__ == '__main__':
test()