forked from markmbadham/pythoncourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbtest.py.bak
50 lines (43 loc) · 1.3 KB
/
dbtest.py.bak
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
import sqlite3 as db
import traceback as tb
class UserDB(object):
def __init__(self):
self.conn = db.connect('test1.db')
self.cur = self.conn.cursor()
def query(self,sql):
try:
self.cur.execute(sql)
except db.DatabaseError:
print "Error in %s" % sql
print tb.format_exc()
else: print "query succeeded"
def setup(self):
sql = '''
CREATE TABLE users(
id INT PRIMARY KEY,
username VARCHAR(64),
password VARCHAR(64))
'''
self.query(sql)
def addrow(self,id,username,password):
sql = '''
INSERT INTO users
VALUES(?, ?,?)'''
self.cur.executemany(sql,[(id,username,password)])
self.conn.commit()
def showtable(self):
sql = "SELECT * FROM users"
self.query(sql)
out = '\t'.join(map(lambda x:x[0],self.cur.description))
for row in self.cur:
out += '\n' + '\t'.join(map(str,row))
return out
#for row in self.cur:
# print row
def getcols(self):
sql = "PRAGMA table_info('users')"
return self.query(sql)
if __name__ == "__main__":
userdb = UserDB()
#userdb.addrow(4,'harry','qwerty')
print userdb.showtable()