Python Forum
Running Sql query stored in CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running Sql query stored in CSV file
#11
Output of line 6 >>>>>>>>>>> select 1 from dual;


For option 1 I am getting below error:

Traceback (most recent call last):
File "oracle_test.py", line 18, in <module>
c.execute(filer)
cx_Oracle.DatabaseError: ORA-00933: SQL command not properly ended

Output of line 6 >>>>>>>>>>> select 1 from dual;


For option 1 I am getting below error:

Traceback (most recent call last):
File "oracle_test.py", line 18, in <module>
c.execute(filer)
cx_Oracle.DatabaseError: ORA-00933: SQL command not properly ended
Reply
#12
Shouldn't you have to set up the file like PL/SQL?
with declare, begin and end Example here: https://docs.oracle.com/cd/B13789_01/app...06_ora.htm
cx_oracle is supposed to be able to run these queries directly from the file
(looking at docs here): https://oracle.github.io/python-cx_Oracle/
Reply
#13
Hi Larz ,
Thanks for explaining it . I know Why this is happening but I am not getting how to remove ' from string .
I will share updated code .
import cx_Oracle
from datetime import datetime


#reading query from file 

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

print(repr(filer))
f.close()
#filer3=filer
#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@database'
#setting up connection 
conn = cx_Oracle.connect(conn_str)	
c = conn.cursor()
c.execute(filer2)
start_time = datetime.now()
for row in c:
    print( row)
conn.close()
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))


		





	
Output:
"select * from dual" '"select * from dual"' no same ('X',) Duration: 0:00:01.239932
if u will check there is ' quote attach to it that is why when I trying to run it it is giving me error.
I am trying to remove these single quotes but not getting anything.
Please help me with this
Reply
#14
Output:
"select * from dual" '"select * from dual"'
This output clearly shows that you have double quotes around your SQL statement in the file.
Note that single quotes around the second print output is due to the fact that you print repr() of the string, not they are actually there, so it's not possible to remove them (line #16)
I told you file should look like this
Output:
SELECT * FROM dual;
just in case - don't put new line (i.e. don't hit ENTER) at the end of the line, even if it doesn't matter in SQL

here is Gist
https://gist.github.com/boyank/3b8f8a4c6...d2de45fdcc
Reply
#15
Hi all,
Thanks its working fine now.
I changed the file to

select * from dual
Reply
#16
what was the problem?
Reply
#17
Hi all,
I did changes just wrote
Select * from dual in my text file no spaces no enter anything and it work fine
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "cannot identify image file" part way through running hatflyer 0 666 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Web project and running a .py file emont 0 637 Dec-11-2022, 11:15 PM
Last Post: emont
  batch file for running python scipt in Windows shell MaartenRo 2 1,883 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  rows from sql query need to write to a file as columns sjcsvatt 6 2,383 Oct-09-2021, 12:45 AM
Last Post: snippsat
  write mariadb table rows query to each file? shams 1 1,872 Feb-02-2021, 04:10 PM
Last Post: buran
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 4,637 Jan-14-2021, 08:58 PM
Last Post: snippsat
  WinError 87 while running .exe file Timych 0 2,364 Aug-06-2020, 02:36 PM
Last Post: Timych
  Write SQLite query result to file hjk6734 1 1,933 May-27-2020, 12:17 PM
Last Post: menator01
  problem running python file using cmd panzers 2 2,300 Dec-19-2019, 04:04 PM
Last Post: panzers
  Interpreter and running a .py file give different outputs PythonNPC 5 2,989 Jul-21-2019, 01:07 PM
Last Post: PythonNPC

Forum Jump:

User Panel Messages

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