-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.py
More file actions
55 lines (47 loc) · 1.3 KB
/
db.py
File metadata and controls
55 lines (47 loc) · 1.3 KB
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
#!/usr/bin/python
import MySQLdb
class DB(object):
db = ""
cursor = ""
FGT_Table = "fgt_devices"
def setup(self, Config):
DBHost = Config["DBHost"]
DBUser = Config["DBUser"]
DBPass = Config["DBPass"]
DBName = Config["DBBase"]
try:
self.db = MySQLdb.connect(DBHost,DBUser,DBPass,DBName )
self.cursor = self.db.cursor()
except MySQLdb.Error as e:
if e[0] == 2002:
print "** Error on connecting to Mysql \n** Is MysqlServer running on target host?"
else:
print "** Error: unable to connect to db \n\t {dberror}".format(dberror=e)
exit()
def getAll(self):
sql = "SELECT * FROM {tbName} WHERE STATUS = 0".format(tbName=self.FGT_Table)
try:
self.cursor.execute(sql)
results = self.cursor.fetchall()
return results
except MySQLdb.Error as e:
print "Error: unable to fecth data: %s" % (e)
self.db.close()
def updateDB(self, field, value, ID ):
sql = 'UPDATE {tbName} SET {tbField} = "{tbValue}" WHERE ID = {tbID}'.format(
tbName=self.FGT_Table,
tbField=field,
tbValue=value,
tbID=ID
)
print sql
try:
self.cursor.execute(sql)
results = self.cursor.fetchall()
if type(results) == tuple:
self.db.commit()
return True
except MySQLdb.Error as e:
print "Error: Unable to update field: %s" % (e)
self.db.close
return False