-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBHelper.py
70 lines (63 loc) · 1.6 KB
/
DBHelper.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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import MySQLdb as mdb
import MySQLdb.cursors
class DBHelper:
def __init__(self,host,user,passwd,db=None):
self.host = host
self.user = user
self.passwd = passwd
self.conn = None
self.cursor = None
self.db = db
def __connect(self,db=None):
if db or self.db:
try:
if db:
self.conn = mdb.connect(self.host,self.user,self.passwd,db)
else:
self.conn = mdb.connect(self.host,self.user,self.passwd,self.db,charset='utf8')
except Exception, e:
print e
return
else:
try:
self.conn = mdb.connect(self.host,self.user,self.passwd)
except Exception, e:
print e
return
self.cursor = self.conn.cursor()
#self.cursor = mdb.cursors.DictCursor(self.conn)
def __close(self):
if self.cursor:
try:
self.cursor.close()
except Exception, e:
print e
if self.conn:
try:
self.conn.close()
except Exception, e:
print e
def select(self, query, db=None):
self.__connect(db)
self.cursor.execute(query)
data = self.cursor.fetchall()
self.__close()
return data
def update(self, query, value, db=None):
self.__connect(db)
self.cursor.execute(query, value)
self.conn.commit()
count = self.cursor.rowcount
self.__close()
return count
if __name__ == '__main__':
db = DBHelper('localhost','root','tangbin//','weiboapp')
#for row in db.select('select * from userlist'):
# print row[0]
print "update userlist set sx='%s'"%('male')
count = db.update("update userlist set an=%s,qq=%s,email=%s",['22',None,'[email protected]'])
print count
for row in db.select('select * from userlist'):
print row[2]