pythonsqlpython-3.xsql-inserthana

Using Python How To Insert Data To Sap Hana Table


I connected sap hana table while inserting data to hana table getting below error

Error inserting data: (260, 'invalid column name: LOADDT')

Below is the insert query using python

 tgt_query = '''
INSERT INTO "SAPCUSTOM"."GEL.Apps.Tech::tbl.IBDlyTrend"(LoadDt,FisWeek,InbeVol,Outbol6WCH,Outol6WWC) VALUES (?,?,?,?,?)
'''
val = (LoadDt,FisWeek,InbeVol,Outbol6WCH,Outol6WWC)
try:
    cur.execute(tgt_query, val)
    #conn.commit() 
    conn.commit()
    print('inserted successfully')                    
except Exception as e:
    print("Error inserting invalid email:", e)

in Hana table column name is LoadDt only but why it is showing invalid column i didn't understand

Please suggest your ideas i'm new to this sap hana

If i use %s in values getting % syntax error

tgt_query = '''INSERT INTO "SAPCUSTOM"."GEL.Apps.Tech::tbl.IBDlyTrend"(LoadDt,FiscalWeek,InboundCaseVol,OutboundCaseVol6WCH,OutboundCaseVol6WWC) VALUES (%s,%s,%s,%s,%s) '''

Error : Error inserting data: (257, 'sql syntax error: incorrect syntax near "%": line 1 col 154 (at pos 154)')

Not sure why it is giving 2 different errors invalid column and syntax near

please help me out anyone


Solution

  • It looks like LoadDt cannot be found, because its actual name contains lower case characters. Adding double-quotes ("LoadDt") should help you out here. When not using quotes, HANA will assume that the name is upper case. Also check this for more details.

    The syntax error indicates, that Python does not replace your string placeholders correctly. This is, in a way, unrelated to the database. It is evident, that you cannot simply replace the SQL placeholders (?) by Python placeholders (%s) without further changes to the code. Check this for more details.