How to insert number of records to Hana table using python and delete previous records

39 Views Asked by At

I wrote python script to connect hana table

it is working fine when we have 1 rec from source but if we have no.of rows it is inserting only 1 record because before inserting data we have delete query

using deleting query we can delete previous data before loading

so i try to insert 60 rec from source query to target query but deleting is performing and finally it is inserting only 1 rec

how can insert this 60 rec without delete

Here is the script which i tried

    from hdbcli import dbapi
import datetime

start_time = datetime.datetime.now()
print('start_time:>',start_time)
try:
   
    conn = dbapi.connect(
    address="",
    port=30015,
    user="",
    password="",
    databasename='B1Q'
  
    )

    print("Succesful Connection to Hana Dev")
except:
    print("Unsuccesful Connection to Hana Dev")    

cur = conn.cursor ()

query = '''
# select query
'''

cur.execute(query)
data = cur.fetchall()
#print(data)
#print(data[0],data[1])
for row in data:
    LoadDt = row[0]
    FiscalWeek = row[1]
    InboundCaseVol = row[2]
    OutboundCaseVol6WCH =row[3]
    OutboundCaseVol6WWC =row[4]
    print(LoadDt,FiscalWeek,InboundCaseVol,OutboundCaseVol6WCH,OutboundCaseVol6WWC)
   
    try:
        del_query = '''
            delete from "SAPCUSTOM"."EL.As.Teh::tl.IBDyTrend_PY" where "LoadDt" = to_char(now(), 'YYYYMMDD')
            '''
        cur.execute(del_query)
        print('number of rows deleted', cur.rowcount)
    except Exception as e:
        print("Error deleting query:", e)

    
    try:
        tgt_query = '''INSERT INTO "SAPCUSTOM"."EL.As.Teh::tl.IBDyTrend_PY"("LoadDt","FiscalWeek","InboundCaseVol","OutboundCaseVol6WCH","OutboundCaseVol6WWC") VALUES (?,?,?,?,?) '''
    
        val = (LoadDt,FiscalWeek,InboundCaseVol,OutboundCaseVol6WCH,OutboundCaseVol6WWC)
        cur.execute(tgt_query, val)
        #conn.commit() 
        conn.commit()
        rowcount = cur.rowcount
        print('rowcount:',rowcount)
        print('inserted successfully')                    
    except Exception as e:
        print("Error inserting data:", e)
    end_time = datetime.datetime.now()
    print('end_time:>',end_time)    

sample output:

Succesful Connection to Hana Dev
20240131 202405 51424366 3577513 635639
number of rows deleted 0
rowcount: 1
inserted successfully
end_time:> 2024-01-31 19:46:26.628110
20240131 202406 439227 3593885 649641
number of rows deleted 1
rowcount: 1
inserted successfully
end_time:> 2024-01-31 19:46:27.773477
20240131 202407 2956902 3583758 645062
number of rows deleted 1
rowcount: 1
inserted successfully
end_time:> 2024-01-31 19:46:28.919200
20240131 202408 6913103 3575464 641822
number of rows deleted 1
rowcount: 1
inserted successfully
end_time:> 2024-01-31 19:46:30.068477
20240131 202409 7072494 3315483 596753
number of rows deleted 1
rowcount: 1
inserted successfully
end_time:> 2024-01-31 19:46:31.220339
20240131 202410 7213464 2969162 536974
number of rows deleted 1
rowcount: 1
inserted successfully
end_time:> 2024-01-31 19:46:32.367452
20240131 202411 7842360 2959509 531365
number of rows deleted 1
rowcount: 1
inserted successfully

so it's inserting and deleting at a time but i want delete only previous ran records not current ran records

can anyone suggest your ideas

0

There are 0 best solutions below