-
Notifications
You must be signed in to change notification settings - Fork 21
/
mysql.py
executable file
·85 lines (75 loc) · 2.68 KB
/
mysql.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
import configparser
import os
import pymysql
from sqlalchemy import create_engine
import pandas as pd
from .config import config
class mysql:
@staticmethod
def getDB(connection='default'):
dbcfg=config.getConfig('db',connection)
db = pymysql.connect(
host=dbcfg['host'],
port=int(dbcfg['port']),
user=dbcfg['user'],
password=dbcfg['password'],
db=dbcfg['db'],
charset=dbcfg['charset'],
cursorclass=pymysql.cursors.DictCursor)
return db,db.cursor()
def getDBEngine(connection='default'):
dbcfg=config.getConfig('db',connection)
engine=create_engine('mysql+pymysql://'+dbcfg['user']+':'+dbcfg['password']+'@'+dbcfg['host']+':'+dbcfg['port']+'/'+dbcfg['db']+'?charset='+dbcfg['charset'],encoding='utf-8')
return engine
def toSql(df,table,connection='default'):
engine=config.getDBEngine(connection)
res = df.to_sql(table, engine, index=False, if_exists='append', chunksize=5000)
def selectToList(sql,connection='default'):
result_list=[]
db,cursor = mysql.getDB(connection)
try:
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
result_list.append(row)
except Exception as e:
print(sql+"MySQL Error:%s" % str(e))
db.close()
return result_list
def delete(sql,connection='default'):
db,cursor = mysql.getDB(connection)
try:
cursor.execute(sql)
db.commit()
except Exception as e:
print(sql+"MySQL Error:%s" % str(e))
db.close()
def sqlexec(sql,connection='default'):
db,cursor = mysql.getDB(connection)
print("Run Sql:"+sql)
try:
cursor.execute(sql)
db.commit()
except Exception as e:
print(sql+"MySQL Error:%s" % str(e))
db.close()
def selectToDf(sql,connection='default'):
db,cursor = mysql.getDB(connection)
results=pd.DataFrame()
try:
cursor.execute(sql)
results = cursor.fetchall()
results= pd.DataFrame(list(results))
except Exception as e:
print(sql+"MySQL Error:%s" % str(e))
db.close()
return results
def truncateTable(table,connection='default'):
db,cursor = mysql.getDB(connection)
try:
sql=" truncate table "+table
cursor.execute(sql)
except Exception as e:
print(sql+"MySQL Error:%s" % str(e))
db.close()
return True