From 14172e3b8183a5be66cdec6988949ec5ea253c44 Mon Sep 17 00:00:00 2001 From: Mike Gerber Date: Wed, 4 Jun 2025 20:32:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Save=20Python=20types=20for=20la?= =?UTF-8?q?ter=20conversion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mods4pandas/lib.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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.