Python Forum

Full Version: how to set echo ON in Python call to Oracle SQL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I am beginner in Python, i am trying to access oracle DB from Python program. I am able to get the output with the below code. However, i want the SQL query which i ran along with the output. I am only getting the output. I tried 'set echo on' but its not working. Please help me.

#!/usr/bin/python

##  Imports
from subprocess import Popen, PIPE
import os
import sys

sql1='set echo ON; \n select name from v$database;'
sqlplus = Popen(["sqlplus", "-S", "/", "as", "sysdba"], stdout=PIPE, stdin=PIPE)
sqlplus.stdin.write(sql1);

out, err = sqlplus.communicate()
print out
Output:
Exexution Details: =========== ./test_db.py NAME --------- testdb
´set echo on´ has effect when executing sql from a file. But in this case you are emulating an interactive session. (In a true interactive session echoing would result in lines appearing double.)
You may also consider installing cx_Oracle to connect to an Oracle database.
Hi,

In my current code, my SQL statement is not displaying on the screen, i am getting only the result. is there anyway to display both the query and result?
I dont have access to install CX_ORACLE, will try it to get it installed.

Thanks
Make a seperate file with the query. Let´s say: file.sql. And in the code you could use that sql file.
sqlplus = Popen(["sqlplus", "-S", "/", "as", "sysdba", "@file.sql"], stdout=PIPE, stdin=PIPE)
I guess that should work.
Hi Ibreeden,

That suggestion worked. Thanks for the help.

Output:
./newtest.py select name from v$database NAME --------- TESTDB