APIPythonOpen-Source Solutions

 View Only
Expand all | Collapse all

How to run a query after establishing a db connection in a python script which uses call (rather than a dedicated library)?

  • 1.  How to run a query after establishing a db connection in a python script which uses call (rather than a dedicated library)?

    Posted 11-04-2020 04:08:00 AM

    I am trying to use the subprocess call in Python to open a sqlcl command line and run a basic query.

    from subprocess import call import time #establish connection call(["./bin/sql", "test/test@delphix-test.va2.test.com:1521:vd4"]) time.sleep(5) #run query query = "select * from test where test like '%test%;'" call([query])

    When I run the python script above, the connection is immediately established and I see the sql welcome banners in my terminal. The query does not seem to run though, and I assume I am doing something wrong. I know my query is correct as I can run that exact same query when manually connecting using sqlcl in the terminal. The script for some reason does not call the query and I am not sure why.

    Does anyone know what I am doing wrong? I suspect it might be because when sql establishes the connection the shell starts running a new process and 'call' won't work in that situation?



    ------------------------------
    likith sunny
    DevOps Engineer
    Delphix Community Members
    ------------------------------


  • 2.  RE: How to run a query after establishing a db connection in a python script which uses call (rather than a dedicated library)?
    Best Answer

    Posted 11-04-2020 01:14:00 PM
    Maybe a typo in query:

    "select * from test where test like '%test%;'"

    should be

    "select * from test where test like '%test%';"


    ------------------------------
    Ranzo Taylor
    Technical Services Director
    Delphix
    ------------------------------