-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.py
36 lines (29 loc) · 1.01 KB
/
DB.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
import MySQLdb
import appconfig
class DB:
def __init__(self):
self.connect()
def connect(self):
try:
self.conn = MySQLdb.connect(appconfig.HOST,
appconfig.USER, appconfig.PASS, appconfig.DATABASE)
except (AttributeError, MySQLdb.OperationalError), e:
raise e
def query(self, sql, params=()):
try:
cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(sql, params)
except (AttributeError, MySQLdb.OperationalError) as e:
print 'exception generated during sql connection: ', e
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql, params)
return cursor
def close(self):
try:
if self.conn:
self.conn.close()
else:
print '...No Database Connection to Close.'
except (AttributeError, MySQLdb.OperationalError) as e:
raise e