-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_handler.py
70 lines (63 loc) · 2.55 KB
/
database_handler.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
import pymysql
class DatabaseHandler(object):
def __init__(self, host, username, password, database, port):
self.host = host
self.username = username
self.password = password
self.database = database
self.port = port
self.db = pymysql.connect(self.host, self.username, self.password, self.database, self.port, charset='utf8')
self.cursor = self.db.cursor()
# 增删改数据库操作
def modify_DB(self, sql):
try:
self.cursor.execute(sql)
# log = self.cursor.execute(sql) # 返回 插入数据 条数 可以根据 返回值 判定处理结果
# print(log)
self.db.commit()
return True
except Exception as e:
print(e)
# 发生错误时回滚
self.db.rollback()
return False
# 查数据库操作
def search_DB(self, sql):
try:
self.cursor.execute(sql)
# log = self.cursor.execute(sql) # 返回 查询数据 条数 可以根据 返回值 判定处理结果
# print(log)
data = self.cursor.fetchall() # 返回所有记录列表
except Exception as e:
print(e)
data = None
return data
# 查数据库中某个表是否存在某个条目,存在返回具体值,不存在则返回None
def exist_DB(self, table, entry, numerical):
try:
sql = "select * from "+ table +" where "+ entry +" = \""+ numerical +"\" LIMIT 1"
self.cursor.execute(sql)
col = self.cursor.description
data = self.cursor.fetchone() # 返回指定记录列表
except Exception as e:
print(e)
data = None
col = None
return data, col
# 当程序调用表中某个条目时,该条目的调用计数字段+1
def count_DB(self, table, entry, numerical):
try:
sql = "update " + table + " set call_count=call_count+'1' where "+ entry +" = \""+ numerical +"\" LIMIT 1"
self.cursor.execute(sql)
self.db.commit()
except Exception as e:
print(e)
self.db.rollback()
if __name__ == '__main__':
DbHandle = DataBaseHandler('10.92.6.176', 'root', 'root', 'devry', 3306)
process_info, process_col = DbHandle.exist_DB("bot_config", "id", "LocalTest")
process_dict = {}
for k, i in enumerate(process_col):
_process_dict = {"bot_"+i[0] : process_info[k]}
process_dict = {**process_dict, **_process_dict}
print(process_dict)