diff --git a/src/mods4pandas/lib.py b/src/mods4pandas/lib.py index 8a65901..082ed9a 100644 --- a/src/mods4pandas/lib.py +++ b/src/mods4pandas/lib.py @@ -315,7 +315,8 @@ def column_names_csv(columns): """ return ",".join('"' + c + '"' for c in columns) -current_columns = defaultdict(list) +current_columns: defaultdict = defaultdict(list) +current_columns_types: dict[dict] = defaultdict(dict) def insert_into_db(con, table, d: Dict): """Insert the values from the dict into the table, creating columns if necessary""" @@ -334,6 +335,11 @@ def insert_into_db(con, table, d: Dict): current_columns[table].append(k) con.execute(f'ALTER TABLE {table} ADD COLUMN "{k}"') + # Save types + for k in d.keys(): + if k not in current_columns_types[table]: + current_columns_types[table][k] = type(d[k]).__name__ + # Insert # Unfortunately, Python3's sqlite3 does not like named placeholders with spaces, so we # have use qmark style here.