empty tabl;e before inserting

celery-integration
peter.fong 9 months ago
parent b70443c839
commit 48266fed67

@ -203,8 +203,15 @@ def getextrapolation(extrapolateYears):
def process_excel(filepath): def process_excel(filepath):
df = pd.read_excel(filepath, engine="openpyxl") df = pd.read_excel(filepath, engine="openpyxl")
df.columns = df.columns.str.strip() df.columns = df.columns.str.strip()
conn, cursor = get_db_connection() conn, cursor = get_db_connection()
cursor.execute(
"""
truncate table onderhoud
"""
)
cursor.execute( cursor.execute(
""" """
CREATE TABLE IF NOT EXISTS onderhoud ( 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 _, row in df.iterrows():
for column in row.index: for column in row.index:
if pd.isna(row[column]): if pd.isna(row[column]):
row[column] = "" row[column] = ""
cursor.execute( record = (row["Object code"],
""" row["Omschrijving PO"], row["Vervaldatum"])
INSERT INTO onderhoud
(Object_code, Object_omschrijving, Afdeling_object, PO_Code, Omschrijving_PO, Vervaldatum, Frequentie, if record not in existing_records:
UOM, PO_schema_Niet_gebruikt, Cluster, Locatie, Locatie_omschrijving, Klasse_object, Categorie) cursor.execute(
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """
""", INSERT INTO onderhoud
( (Object_code, Object_omschrijving, Afdeling_object, PO_Code, Omschrijving_PO, Vervaldatum, Frequentie,
row["Object code"], UOM, PO_schema_Niet_gebruikt, Cluster, Locatie, Locatie_omschrijving, Klasse_object, Categorie)
row["Object omschrijving"], VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
row["Afdeling object"], """,
row["PO Code"], (
row["Omschrijving PO"], row["Object code"],
row["Vervaldatum"], row["Object omschrijving"],
row["Frequentie"], row["Afdeling object"],
row["UOM"], row["PO Code"],
row["PO-schema Niet gebruikt"], row["Omschrijving PO"],
row["Cluster"], row["Vervaldatum"],
row["Locatie"], row["Frequentie"],
row["Locatie omschrijving"], row["UOM"],
row["Klasse object"], row["PO-schema Niet gebruikt"],
row["Categorie"], row["Cluster"],
), row["Locatie"],
) row["Locatie omschrijving"],
row["Klasse object"],
row["Categorie"],
),
)
existing_records.add(record)
conn.commit() conn.commit()
cursor.close() cursor.close()

Loading…
Cancel
Save