from app.models import Annual_Data, Glacier, User 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): models = [Glacier, Annual_Data, User] for model in models: if model.query.first() 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()