Python Forum
inserting data into oracle db using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: inserting data into oracle db using python (/thread-3413.html)



inserting data into oracle db using python - sahilsiddharth - May-21-2017

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*


RE: inserting data into oracle db using python - Larz60+ - May-21-2017

show more context, preferably a piece of code that should run (even if not)
don't know why you are using *cursor.execute


RE: inserting data into oracle db using python - sahilsiddharth - May-21-2017

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



RE: inserting data into oracle db using python - Ofnuts - May-21-2017

(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.


RE: inserting data into oracle db using python - sahilsiddharth - May-22-2017

same error


RE: inserting data into oracle db using python - jogl - May-22-2017

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()



RE: inserting data into oracle db using python - sahilsiddharth - May-22-2017

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



RE: inserting data into oracle db using python - jogl - May-22-2017

Check on the %s placeholder.  You may need to use a different letter there.


RE: inserting data into oracle db using python - Ofnuts - May-22-2017

(May-22-2017, 02:45 PM)sahilsiddharth Wrote: same error

Please show the new code.


RE: inserting data into oracle db using python - nilamo - May-22-2017

(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, ))