Python Forum

Full Version: inserting data into oracle db using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my question here

my code here
I am unable to insert date into table by using variable in python 3.6.1. 
If i pass the value hard-coded, it is working, but the same is not working if i give it a variable name.

Below line of code is working :
**cursor.execute("INSERT INTO telm  VALUES('rahul')")**

But if i pass as variable :
*cursor.execute("INSERT INTO telm  VALUES(fname)")...giving error ORA-00984: column not allowed here*
show more context, preferably a piece of code that should run (even if not)
don't know why you are using *cursor.execute
import sys
import cx_Oracle

class Customer():    
   def createCustomer(self,fname):
       print ("name is %s" % fname)
       db = cx_Oracle.connect('system/12345678@localhost/XE' )
       cursor=db.cursor()
       cursor.execute("INSERT INTO telm  VALUES (fname)")
       db.commit()

fname=input("Enter your first name :")
c1=Customer();
c1.createCustomer(fname)
     

output---


Output:
Enter your first name :rahul name is rahul Traceback (most recent call last):  File "E:\python\test_cust.py", line 14, in <module>    c1.createCustomer(fname)  File "E:\python\test_cust.py", line 9, in createCustomer    cursor.execute("INSERT INTO telm  VALUES (fname)") cx_Oracle.DatabaseError: ORA-00984: column not allowed here
(May-21-2017, 08:47 PM)sahilsiddharth Wrote: [ -> ]import sys
import cx_Oracle

class Customer():    
   def createCustomer(self,fname):
       print ("name is %s" % fname)
       db = cx_Oracle.connect('system/12345678@localhost/XE' )
       cursor=db.cursor()
       cursor.execute("INSERT INTO telm  VALUES (fname)")
       db.commit()

fname=input("Enter your first name :")
c1=Customer();
c1.createCustomer(fname)
     

output---

Enter your first name :rahul
name is rahul
Traceback (most recent call last):
 File "E:\python\test_cust.py", line 14, in <module>
   c1.createCustomer(fname)
 File "E:\python\test_cust.py", line 9, in createCustomer
   cursor.execute("INSERT INTO telm  VALUES (fname)")
cx_Oracle.DatabaseError: ORA-00984: column not allowed here

See the "Bind Variable Patterns" section in this page.
same error
This kind of thing works for me:

               with conn2:
                        cur2=conn2.cursor()
                        cur2.execute("""INSERT INTO boiltbl VALUES(%s,%s,%s,%s,%s,%s,%s)""", (time_now2,
                                                reply_s[4],reply_s[5],reply_s[2],reply_s[3],reply_s[0],reply_s[11]))
                        conn2.commit()
now getting below error :


Error:
Enter your first name :ss name is ss Traceback (most recent call last):  File "E:\python\test_cust.py", line 14, in <module>    c1.createCustomer(a)  File "E:\python\test_cust.py", line 9, in createCustomer    cursor.execute("""INSERT INTO TELM VALUES(%s)""", (a)) cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number
Check on the %s placeholder.  You may need to use a different letter there.
(May-22-2017, 02:45 PM)sahilsiddharth Wrote: [ -> ]same error

Please show the new code.
(May-22-2017, 03:34 PM)sahilsiddharth Wrote: [ -> ]cursor.execute("""INSERT INTO TELM VALUES(%s)""", (a))
That's not a tuple, it's just a. Because it's a single item, you need to add a comma to force a single-element tuple, like so:
cursor.execute("""insert into telm values (%s)""", (a, ))