Python Forum
how to set echo ON in Python call to Oracle SQL - 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: how to set echo ON in Python call to Oracle SQL (/thread-21202.html)



how to set echo ON in Python call to Oracle SQL - nmrt - Sep-19-2019

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



RE: how to set echo ON in Python call to Oracle SQL - ibreeden - Sep-19-2019

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


RE: how to set echo ON in Python call to Oracle SQL - nmrt - Sep-20-2019

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


RE: how to set echo ON in Python call to Oracle SQL - ibreeden - Sep-20-2019

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.


RE: how to set echo ON in Python call to Oracle SQL - nmrt - Sep-21-2019

Hi Ibreeden,

That suggestion worked. Thanks for the help.

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