-
Notifications
You must be signed in to change notification settings - Fork 2
/
DBHelper.py
114 lines (101 loc) · 2.7 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
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
#!/usr/bin/python
#-*- coding: utf-8 -*-
import MySQLdb as mdb
import MySQLdb.cursors
import sys
import utility
reload(sys)
sys.setdefaultencoding('utf8')
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 select_one(self, query, db=None):
self.__connect(db)
self.cursor.execute(query)
data = self.cursor.fetchone()
self.__close()
return data
def insert(self, query, value, db=None):
self.__connect(db);
self.cursor.execute(query, value);
rowid = self.cursor.lastrowid;
self.conn.commit();
self.__close();
return rowid;
def insert_many(self, query, values, db=None):
self.__connect(db);
self.cursor.executemany(query ,values);
rowcount = self.cursor.rowcount
self.conn.commit();
self.__close();
return rowcount;
def __getRC(self):
if(hasattr(self.cursor, 'rowcount')):
return self.cursor.rowcount;
else:
return -1;
def update(self, query, value, db=None):
self.__connect(db)
self.cursor.execute(query, value)
self.conn.commit()
count = self.__getRC();
self.__close()
return count
def delete(self, query, value, db=None):
self.__connect(db)
self.cursor.execute(query, value)
self.conn.commit()
count = self.__getRC();
self.__close()
return count
def get_last_query(self):
return self.cursor._last_executed;
if __name__ == '__main__':
db = DBHelper('localhost','root','tangbin//','ecshop');
#print db.insert_many("INSERT INTO ecs_brand (brand_name) VALUES (%s)", [['test7'],['test8']]);
#db.delete("DELETE FROM ecs_brand WHERE brand_name=%s", ['test9']);
#print db.get_last_query();
#res = db.select_one("SELECT count(brand_name) FROM ecs_brand WHERE brand_name='%s'"%'诺基亚');
#print res[0];
update_mt = '175,599'
db.update("UPDATE `abbreviation` SET mt=%s WHERE abrid IN (" + update_mt + ")", [utility.now()]);