-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_db.py
156 lines (128 loc) · 3.77 KB
/
mysql_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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pymysql
import json
import logging
import traceback
from datetime import datetime
mysql_config_local = {
"host": "*",
"port": 3306,
"username": "*",
"password": "*",
"db_name": "*",
"charsets": "UTF8"
}
class MysqlUtil(object):
"""mysql util"""
db = None
cursor = True
def __init__(self, db_name=None):
self.host = mysql_config_local.get("host")
self.port = mysql_config_local.get("port")
self.username = mysql_config_local.get("username")
self.password = mysql_config_local.get("password")
self.db_name = db_name if db_name else mysql_config_local.get(
"db_name")
self.charsets = mysql_config_local.get("charsets")
def get_con(self):
"""[连接数据库]
"""
self.db = pymysql.Connect(host=self.host, port=self.port,
user=self.username, password=self.password, database=self.db_name)
self.cursor = self.db.cursor()
def close(self):
"""[关闭数据库]
"""
self.cursor.close()
self.db.close()
def show_tables(self):
"""[展示所有表]
"""
res = {}
try:
self.get_con()
self.cursor.execute("show tables")
res = {tp[0]: i for i, tp in enumerate(self.cursor.fetchall())}
self.close()
except Exception as e:
print("show tables error --> "+str(e))
logging.error(traceback.format_exc())
return res
def find_one(self, sql):
res = None
try:
self.get_con()
self.cursor.execute(sql)
res = self.cursor.fetchone()
self.close()
except Exception as e:
print("query error!" + str(e))
return res
def find_all(self, sql):
res = None
try:
self.get_con()
self.cursor.execute(sql)
res = self.cursor.fetchall()
self.close()
except Exception as e:
print("query error!" + str(e))
return res
def __insert(self, sql):
count = 0
try:
self.get_con()
count = self.cursor.execute(sql)
self.db.commit()
self.close()
except Exception as e:
print("操作失败!" + str(e))
self.db.rollback()
return count
def save(self, sql):
"""[保存数据]
Args:
sql ([string]): [sql语句]
Returns:
[integer]: [操作数量]
"""
return self.__insert(sql)
def update(self, sql):
"""[更新数据]
Args:
sql ([string]): [sql语句]
Returns:
[integer]: [操作数量]
"""
return self.__insert(sql)
def delete(self, sql):
"""[删除数据]
Args:
sql ([string]): [sql语句]
Returns:
[integer]: [操作数量]
"""
return self.__insert(sql)
if __name__ == '__main__':
from pprint import pprint
mysql_util = MysqlUtil()
# print("对象实例后的属性:"+json.dumps(mysql_util.__dict__))
# 主键查询
# sql = "select * from zccf_policy where policy_id=405075"
# doc = mysql_util.find_one(sql)
# print(doc)
# 列表查询
# sql = "select * from zccf_policy_topic"
# docs = mysql_util.find_all(sql)
# pprint(docs)
# pprint(mysql_util.show_tables())
# 插入
# 更新
# 删除
# sql = "SELECT DICT_VALUE,DICT_NAME FROM zccf_sys_dict WHERE DICT_KEY='SXMK_MATTER_SUPPORT'"
# doc = mysql_util.find_all(sql)
# pprint(doc)
sql = "SELECT ORGCODE,ORGNAME FROM s11t1_gov_org"
doc = mysql_util.find_all(sql)
pprint(doc)