-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
72 lines (62 loc) · 1.65 KB
/
database.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb
import thread
class DataBase():
def __init__(self):
self.db_host = 'localhost'
self.db_user = 'root'
self.db_passwd = 'password'
self.db = None
self.cursor = None
self.lock = thread.allocate_lock()
def connect(self):
try:
self.db = MySQLdb.connect(host=self.db_host, user=self.db_user, passwd=self.db_passwd, db='qjwgg', charset='utf8')
self.cursor = self.db.cursor()
except:
print 'connect mysql failed!'
raise
def disconnect(self):
try:
if self.cursor is not None:
self.cursor.close()
if self.db is not None:
self.db.close()
except:
pass
def reconnect(self):
try:
self.disconnect()
self.connect()
except:
pass
def _ping(self):
try:
self.db.ping()
except:
self.reconnect()
def select(self, sql): ## select
results = []
try:
self.execute(sql)
results = self.cursor.fetchall()
except Exception, e:
pass
finally:
return results
def execute(self, sql): ## insert/update/delete/replace
try:
self.lock.acquire()
self._ping()
self.cursor.execute(sql)
except Exception, e:
pass
finally:
self.lock.release()
def commit(self):
try:
self.db.commit()
except:
self.db.rollback()
dbhandler = DataBase()