igdb/code/database/export.py

41 lines
1.0 KiB
Python
Raw Normal View History

2020-01-08 01:23:00 +01:00
from 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
2020-01-06 08:31:21 +01:00
2020-01-06 11:12:55 +01:00
def create_dataframes() -> DataFrame:
files = {
"glacier": "../data/glacier.csv",
2020-01-08 01:47:12 +01:00
"annual_data": "../data/annual_data.csv",
"annual_change": "../data/annual_change.csv",
"user": "../data/user.csv",
2020-01-06 11:12:55 +01:00
}
df_list = {}
for csv in files.keys():
2020-01-08 01:23:00 +01:00
df_list[csv] = read_csv(files[csv])
2020-01-06 11:12:55 +01:00
return df_list
2020-01-06 08:31:21 +01:00
2020-01-06 11:12:55 +01:00
def insert_data(df_list, conn):
for key, value in df_list.items():
2020-01-08 01:47:12 +01:00
value.to_sql(key, con=conn, index=False, if_exists="append")
2020-01-06 08:31:21 +01:00
def main():
conn = create_connection()
2020-01-06 11:12:55 +01:00
df_list = create_dataframes()
insert_data(df_list, conn)
2020-01-06 08:31:21 +01:00
if __name__ == "__main__":
main()