-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoolkit_mysqldb.py
138 lines (119 loc) · 3.85 KB
/
toolkit_mysqldb.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
import MySQLdb
# import csv
import os
import sys
import logging
import pandas as pd
class MySQL(object):
def __init__(self, host, port, user, passwd, db, charset='utf8'):
self.conn = MySQLdb.connect(
host=host,
port=port,
user=user,
passwd=passwd,
db=db,
charset=charset)
def __enter__(self):
return self
def __exit__(self, Type, value, traceback):
'''
Executed after "with"
'''
if hasattr(self, 'self.cursor'):
logging.info('Close the DB')
self.cursor.close()
def get_cursor(self):
return self.conn.cursor()
def query(self, sql):
cursor = self.get_cursor()
try:
cursor.execute(sql, None)
result = cursor.fetchall()
except Exception as e:
logging.error("mysql query error: %s", e)
return None
finally:
cursor.close()
return result
def query2dict(self, sql):
cursor = self.get_cursor()
try:
cursor.execute(sql, None)
desc = cursor.description
column_names = [col[0] for col in desc]
data = [dict(zip(column_names, row))
for row in cursor.fetchall()]
except Exception as e:
logging.error("mysql query error: %s", e)
return None
finally:
cursor.close()
return data
def query2dataframe(self, sql):
cursor = self.get_cursor()
try:
df = pd.read_sql(sql, self.conn)
except Exception as e:
logging.error("mysql query error: %s", e)
return None
finally:
cursor.close()
return df
def execute(self, sql, param=None):
affected_row = 0
cursor = self.get_cursor()
try:
cursor.execute(sql, param)
self.conn.commit()
affected_row = cursor.rowcount
except Exception as e:
logging.error("mysql execute error: %s", e)
return 0
finally:
cursor.close()
logging.info('affected_rows: ' + str(affected_row))
return affected_row
def executemany(self, sql, params=None):
cursor = self.get_cursor()
affected_rows = 0
try:
cursor.executemany(sql, params)
self.conn.commit()
affected_rows = cursor.rowcount
except Exception as e:
logging.error("mysql executemany error: %s", e)
return 0
finally:
cursor.close()
logging.info('affected_rows: ' + str(affected_rows))
return affected_rows
def close(self):
try:
self.conn.close()
except Exception as e:
pass
def __del__(self):
self.close()
if __name__ == '__main__':
import configparser
config_file = os.path.dirname(
os.path.realpath(__file__)) + os.sep + 'config.ini'
try:
logging.debug('Read config file: ' + config_file)
configRead = configparser.ConfigParser()
configRead.read(config_file)
config = {
'host': configRead['database']['host'],
'port': int(configRead['database']['port']),
'user': configRead['database']['user'],
'passwd': configRead['database']['passwd'],
'db': configRead['database']['db'],
'charset': 'utf8'
}
except Exception as e:
logging.error("Error read config file, check config.ini")
sys.exit(1)
query_sql = '''SELECT * FROM Other.test;'''
with MySQL(**config) as mysql:
result = mysql.query2dataframe(query_sql)
print(result)