From 48266fed67af2fb267b73a7fe47b6580d78b55c1 Mon Sep 17 00:00:00 2001 From: "peter.fong" Date: Sun, 16 Mar 2025 20:46:31 +0000 Subject: [PATCH] empty tabl;e before inserting --- delfland/ppo-insight/ppo_insight.py | 64 ++++++++++++++++++----------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/delfland/ppo-insight/ppo_insight.py b/delfland/ppo-insight/ppo_insight.py index 114f0af..3adc38a 100644 --- a/delfland/ppo-insight/ppo_insight.py +++ b/delfland/ppo-insight/ppo_insight.py @@ -203,8 +203,15 @@ def getextrapolation(extrapolateYears): def process_excel(filepath): df = pd.read_excel(filepath, engine="openpyxl") df.columns = df.columns.str.strip() + conn, cursor = get_db_connection() + cursor.execute( + """ + truncate table onderhoud + """ + ) + cursor.execute( """ CREATE TABLE IF NOT EXISTS onderhoud ( @@ -227,35 +234,44 @@ def process_excel(filepath): """ ) + cursor.execute( + "SELECT Object_code, Omschrijving_PO, Vervaldatum FROM onderhoud") + existing_records = set(cursor.fetchall()) + for _, row in df.iterrows(): for column in row.index: if pd.isna(row[column]): row[column] = "" - cursor.execute( - """ - INSERT INTO onderhoud - (Object_code, Object_omschrijving, Afdeling_object, PO_Code, Omschrijving_PO, Vervaldatum, Frequentie, - UOM, PO_schema_Niet_gebruikt, Cluster, Locatie, Locatie_omschrijving, Klasse_object, Categorie) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - """, - ( - row["Object code"], - row["Object omschrijving"], - row["Afdeling object"], - row["PO Code"], - row["Omschrijving PO"], - row["Vervaldatum"], - row["Frequentie"], - row["UOM"], - row["PO-schema Niet gebruikt"], - row["Cluster"], - row["Locatie"], - row["Locatie omschrijving"], - row["Klasse object"], - row["Categorie"], - ), - ) + record = (row["Object code"], + row["Omschrijving PO"], row["Vervaldatum"]) + + if record not in existing_records: + cursor.execute( + """ + INSERT INTO onderhoud + (Object_code, Object_omschrijving, Afdeling_object, PO_Code, Omschrijving_PO, Vervaldatum, Frequentie, + UOM, PO_schema_Niet_gebruikt, Cluster, Locatie, Locatie_omschrijving, Klasse_object, Categorie) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """, + ( + row["Object code"], + row["Object omschrijving"], + row["Afdeling object"], + row["PO Code"], + row["Omschrijving PO"], + row["Vervaldatum"], + row["Frequentie"], + row["UOM"], + row["PO-schema Niet gebruikt"], + row["Cluster"], + row["Locatie"], + row["Locatie omschrijving"], + row["Klasse object"], + row["Categorie"], + ), + ) + existing_records.add(record) conn.commit() cursor.close()