pythonsubprocess

Set echo ON in Python call to Oracle SQL is not working


I am beginner in Python, I am trying to access an Oracle database from my Python program. I am able to get the output with the code shown here. 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 it's 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

Current Output:

=================
Execution Details:
===========
 ./test_db.py
NAME
---------
testdb

I want the SQL query also to be printed along with output as below


Solution

  • Its fixed after using SQL file instead of direct commands.

    sqlplus = Popen(["sqlplus", "-S", "/", "as", "sysdba", "@file.sql"], stdout=PIPE, stdin=PIPE)