Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parameterized SQL query
#1
Hi Experts,

I have one sql file(workphone.sql) which has one query written in that:

query >>


SELECT * FROM <TABLE_NAME> WHERE <COLUMN_NAME>=? AND <COLUMN_NAME2> IS NOT NULL

Now I am calling this in separate script
import cx_Oracle
from datetime import datetime


#reading query from file 

#f= open('C:/Users/pubhatia/Documents/learning/python/query/test.sql')
f = open('C:/Users/pubhatia/Documents/learning/python/query/work_phone.sql')
#print(f.read())
filer = f.read()
print(filer)

print(repr(filer))
f.close()
#filer3=filer
#source_name ='PWS'
source_name= ('PWS')
#filer=filer.replace("\'","")


	#print( repr(filer3 )		
filer2="select * from dual"

if filer==filer2:
 print('same value')
else:
 print ('no same ')

#create connection string
conn_str = u'user/pwd@db'
	#setting up connection 
conn = cx_Oracle.connect(conn_str)	
c = conn.cursor()
	#c.execute("select WORK_PHONE,source_sys_cd,source_sys_id  from mdm_people_standard where source_sys_cd ='%s' AND  WORK_PHONE IS NOT NULL"%(source_name))
c.execute(filer,'PWS')
start_time = datetime.now()
for row in c:
	print(row)
conn.close()
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))


Now When I am using 3 formats I am getting error for all format

source_name= ('PWS',)
c.execute(filer,source_name)
source_name= ('PWS')
c.execute(filer,source_name)
source_name= ('PWS')
c.execute(filer,'PWS')
error I am getting for all scenario

Error:
Traceback (most recent call last): File "oracle_test.py", line 35, in <module> c.execute(filer,source_name) cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number
Please confirm how can we pass value in parameterised sql

Hi All,
I did some changes mentioned in below given link and its working fine

http://www.oracle.com/technetwork/articl...91105.html

If anyone has better resolution do let me now
Reply


Messages In This Thread
Parameterized SQL query - by purnima1 - Oct-10-2017, 01:15 PM
RE: Parameterized SQL query - by nilamo - Oct-10-2017, 03:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Parameterized math calculations? babaliaris 7 17,074 Jun-01-2022, 10:19 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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