Python Forum
how to set echo ON in Python call to Oracle SQL
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to set echo ON in Python call to Oracle SQL
#1
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
Reply
#2
´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.
Reply
#3
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
Reply
#4
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.
Reply
#5
Hi Ibreeden,

That suggestion worked. Thanks for the help.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to make my Alexa/Echo dot 3 powered by ChatGPT.... mariozio 1 1,008 Apr-20-2023, 05:24 PM
Last Post: farshid
  Encrypting Oracle Passwords / Python Library for That? bmccollum 1 2,595 Jun-11-2021, 07:59 PM
Last Post: Larz60+
  disable subprocess.popen prompt echo paul18fr 1 2,017 Feb-04-2021, 02:50 AM
Last Post: Larz60+
  Calling Oracle REST SQL from Python johnjacob 2 2,040 Nov-05-2020, 04:19 AM
Last Post: johnjacob
  Python to Oracle Conn Issue chvsnarayana 2 39,219 Sep-06-2020, 04:33 PM
Last Post: Larz60+
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 6,510 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  Load table from Oracle to MYSQL using python himupant94 0 1,644 May-12-2020, 04:50 PM
Last Post: himupant94
  Call pip3 from python folder build by me call pip3 from the /usr/bin Suryavarman 3 3,692 Oct-07-2019, 10:23 PM
Last Post: Suryavarman
  Long command with characters not working in Python on Oracle Linux 7 iaas_infra 10 6,252 Jul-19-2019, 04:53 PM
Last Post: ichabod801
  Echo call to VLC bash using subprocess.Popen on Linux LuukS001 17 9,699 Jan-07-2019, 03:58 AM
Last Post: LuukS001

Forum Jump:

User Panel Messages

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