diff --git a/code/database/export.py b/code/database/export.py index 266cb06..98e2817 100644 --- a/code/database/export.py +++ b/code/database/export.py @@ -25,6 +25,8 @@ def create_dataframes() -> DataFrame: 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") diff --git a/code/database/parser.py b/code/database/parser.py index fa66be8..f2861cd 100644 --- a/code/database/parser.py +++ b/code/database/parser.py @@ -2,6 +2,7 @@ from iso3166 import countries as co from pandas import DataFrame, concat, read_csv from csv import QUOTE_NONNUMERIC from database.constants import ADMIN_PW +from os import path def country_conversion(political_unit) -> str: @@ -51,12 +52,12 @@ def rename_fields(df_list): def create_databases(df): - user = {"uid": [7843], "username": ["admin"], "password": [ADMIN_PW]} files = { "glacier": "../data/glacier.csv", "annual_data": "../data/annual_data.csv", "user": "../data/user.csv", } + user = {"uid": [7843], "username": ["admin"], "password": [ADMIN_PW]} dataframes = { "glacier": df[["POLITICAL_UNIT", "NAME", "WGMS_ID"]].drop_duplicates(), "annual_data": df[["WGMS_ID", "YEAR", "AREA", "MEDIAN_ELEVATION", "LENGTH"]], @@ -64,6 +65,8 @@ def create_databases(df): } renamed_dfs = rename_fields(dataframes) for key, val in renamed_dfs.items(): + if path.isfile(files[key]): + continue val.to_csv(files[key], index=False, quoting=QUOTE_NONNUMERIC)