-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connect.py
More file actions
55 lines (48 loc) · 1.96 KB
/
db_connect.py
File metadata and controls
55 lines (48 loc) · 1.96 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
import configparser
import MySQLdb
from sqlalchemy import create_engine
import pandas as pd
class DatabaseConnect():
def __init__(self,database,hostname,username,password,port):
self.hostname = hostname
self.username = username
self.password = password
self.port = port
self.database = database
self.engine = create_engine("mysql+pymysql://{}:{}@{}:{}/{}".format(self.username,
self.password,
self.hostname,
self.port,
self.database))
def connection_obj(self):
connection = self.engine.connect()
return connection
def query(self,sql_str,df_flag=True,parse_dates=None,chunksize=None,columns=None):
'''
sql_str(str): query to be fetched from data base
returns a pandas dataframe that contains query results
'''
try:
connection = self.connection_obj()
if df_flag == True:
df = self.to_dataframe(sql_str,connection,parse_dates,chunksize=None)
return df
else:
connection.execute(sql_str)
connection.close()
except:
raise Exception
def to_dataframe(self,sql_str, connection, parse_dates=None, chunksize=None):
df = pd.read_sql(sql_str,con=connection,parse_dates=parse_dates,chunksize=None)
return df
def insert(self,df,tbl,chunksize=None):
'''
df(pd.DataFrame): df to be inserted
tbl(str): table in database to insert df into
'''
try:
connection = self.connection_obj()
df.to_sql(name=tbl,con=connection,if_exists='append',index=False,chunksize=None)
connection.close()
except:
raise Exception