Insert only new entries in a sql table in python with hdbcli dbapi

93 Views Asked by At

I have a SQL table (based on SAP HANA) with the following code:

from hdbcli import dbapi
conn = dbapi.connect(
        address  = os.getenv('DSP_ADDRESS'),
        port     = os.getenv('DSP_PORT'),
        user     = os.getenv('DSP_USER'),
        password = os.getenv('DSP_PASSWORD'))
cursor = conn.cursor()
output_data = list(df.itertuples(index=False, name=None))
sql_insert = 'INSERT INTO "DATABASE#PYTHON"."OUTLIER" VALUES (?,?,?,?,?,?,?)'
cursor.executemany(sql_insert, output_data)
conn.commit()

The dataframe df will add some new rows every months. How can I only insert the new rows, which are not existing in the current sql table? And second question, how can I exclude some columns from the checkup? So for example:

SQL
Index  Value  Calendar
1       10      1
2       20      2
3       10      3

And I have the dataframe

df
Index  Value  Calendar
1       10      1
2       20      2
4       10      3
5       40      4

So I want only to insert the new row:

Index  Value  Calendar
5       40      4

into the existing sql table?

0

There are 0 best solutions below