42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
from database.constants import DB_NAME, DB_PW, DB_USER
|
|
from pandas import DataFrame, read_csv
|
|
from sqlalchemy import create_engine, engine
|
|
|
|
|
|
def create_connection() -> engine:
|
|
host = "localhost:3306"
|
|
connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format(
|
|
user=DB_USER, pw=DB_PW, url=host, db=DB_NAME
|
|
)
|
|
engine = create_engine(connection_uri)
|
|
return engine
|
|
|
|
|
|
def create_dataframes() -> DataFrame:
|
|
files = {
|
|
"glacier": "../data/glacier.csv",
|
|
"annual_data": "../data/annual_data.csv",
|
|
"user": "../data/user.csv",
|
|
}
|
|
df_list = {}
|
|
for csv in files.keys():
|
|
df_list[csv] = read_csv(files[csv])
|
|
return df_list
|
|
|
|
|
|
def insert_data(df_list, conn):
|
|
if conn.table_names() is not None:
|
|
return
|
|
for key, value in df_list.items():
|
|
value.to_sql(key, con=conn, index=False, if_exists="append")
|
|
|
|
|
|
def main():
|
|
conn = create_connection()
|
|
df_list = create_dataframes()
|
|
insert_data(df_list, conn)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|