Search code examples
pythonpython-3.xoracle-databasecx-oracle

ORA-01036: illegal variable name/number (cx_Oracle)


When I am trying to insert data into DB and i can see the above error when i am using below code.

Could you please suggest what else can be done.

Code:

list_to_add=['Have you searched','similar question has already been posted']

dsn_tns = cx.makedsn(cred_test['HOST'], cred_test['PORT'], service_name=cred_test['SERVICE_NAME'])

conn = cx.connect(user=cred_test['USER'], password=cred_test['PASWRD'], dsn=dsn_tns)

cursor = conn.cursor()

cursor.prepare('INSERT INTO Table_name Col_name values (:0)')

cursor.executemany(None,list_to_add)

conn.commit()

Solution

  • You have minor issues :

    1. ORA-01036 raises due to list elements are not wrapped up with square brackets

    2. ORA-00947 will raise after fixing the first matter, since the Col_name is not wrapped up with parentheses within the Insert statement

      list_to_add=[['Have you searched'],['similar question has already been posted']]
      
      dsn_tns = cx.makedsn(cred_test['HOST'], cred_test['PORT'], service_name=cred_test['SERVICE_NAME'])
      
      conn = cx.connect(user=cred_test['USER'], password=cred_test['PASWRD'], dsn=dsn_tns)
      
      cursor = conn.cursor()
      
      cursor.prepare('INSERT INTO Table_name(Col_name) VALUES(:0)')
      
      cursor.executemany(None,list_to_add)
      
      conn.commit()