Python Forum
inserting data into oracle db using python
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
inserting data into oracle db using python
#1
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*
Reply
#2
show more context, preferably a piece of code that should run (even if not)
don't know why you are using *cursor.execute
Reply
#3
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
Reply
#4
(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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
same error
Reply
#6
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()
Reply
#7
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
Reply
#8
Check on the %s placeholder.  You may need to use a different letter there.
Reply
#9
(May-22-2017, 02:45 PM)sahilsiddharth Wrote: same error

Please show the new code.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#10
(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, ))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Load data in Oracle muzokh 0 258 Mar-08-2024, 11:19 PM
Last Post: muzokh
  python script for inserting rows into hbase table lravikumarvsp 7 7,115 Mar-24-2023, 04:44 AM
Last Post: parth_botadara
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,392 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Migrating data from oracle into postgres python_student 1 2,449 Feb-10-2022, 09:16 PM
Last Post: buran
  Encrypting Oracle Passwords / Python Library for That? bmccollum 1 2,596 Jun-11-2021, 07:59 PM
Last Post: Larz60+
  Load data from One oracle Table to Multiple tables amy83 1 1,790 Dec-02-2020, 01:57 AM
Last Post: Larz60+
  Calling Oracle REST SQL from Python johnjacob 2 2,046 Nov-05-2020, 04:19 AM
Last Post: johnjacob
  Python to Oracle Conn Issue chvsnarayana 2 39,674 Sep-06-2020, 04:33 PM
Last Post: Larz60+
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 6,517 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  Matching Regex in Python from Excelfile and inserting into database Shuzo 0 1,632 May-14-2020, 06:53 PM
Last Post: Shuzo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020