Forum Stats

  • 3,876,133 Users
  • 2,267,067 Discussions


Can I just issue sqlplus command like a Korn shell does in python?

389975 Member Posts: 55
edited Jan 15, 2009 1:44PM in Python

I have a question on python as an Oracle DBA. Do I have to have cx_oracle or another similar compenent in order to access Orade database? Can I just do sqlplus like a Korn shell does?

I am an Oracle DBA. Daily job requires shell scripting. I am skilful at Korn/BASH which I use on a daily basis to interact with database. However, I found them not powerful and flexible enough to handle text and strings. I am new to both Perl and Python. I pick Python because it is easy to read and maintain. Learning curve is not as steep as Perl.

I am not a developer who cares much about data types compatability or APIs. I do care about how to pass Unix variables in Python. A typical task is like

set Unix environment variables (each database has a line in a file)
use sqlplus to connect to each database specified in the file, i.e. looping
get information I want and store them in files, i.e. logs
end loop
do text/string manipulation againt the log file
if error is found, send email alert to DBA



  • 579841
    579841 Member Posts: 1

    Python has some great libraries to do what you are asking in the Unix enviornment:

    To get enviornmental variables:

    import os
    # store the env var audit_username in a variable; if it doesn't exist, use 'davew' as the default
    username = os.getenv("audit_username",default='davew')

    To run Unix commands:

    import commands
    # run a command from unix (example from Python manual
    commands.getstatusoutput('ls /bin/ls')

    Dave Warner
  • Christopher Jones-Oracle
    Christopher Jones-Oracle Member Posts: 1,656 Employee
    You'll need cx_Oracle - it is the thing that actually connects to Oracle. It is well maintained and easy to use.

    If you want to emulate SQL*Plus in Python, look at:
This discussion has been closed.