0

I have to connect to a sybase database and run a simple select query using python script

On my server isql command can run only from sybase bin directory, so i have to cd to that directory before firing the query.

---------------------------Edited-----------------------------

Uptill now i'm able to do this:-

#!/usr/bin/python
  import subprocess
  path = "path/to/sybase/bin"
  os.chdir(path)
  arguments = ['./isql',"-S server_name", "-U user", "-P password", "-D database","""<<EOF
  SELECT * FROM sometable
  go
  EOF"""]
  ps = subprocess.Popen(arguments)
  out = ps.communicate()
  print out

The errors are just out of my understanding capability :(

Traceback (most recent call last):
File "./test_db.py", line 8, in ?
ps = subprocess.Popen(arguments)
File "/usr/lib64/python2.4/subprocess.py", line 542, in __init__
errread, errwrite)
File "/usr/lib64/python2.4/subprocess.py", line 975, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

I'm able to do this outside my python script using isql command on my unix terminal

how can i use isql in python subprocess module?

4
  • Use import os; os.chdir(path) to change path. Commented May 22, 2014 at 10:26
  • Remove parenthesis from inside string. Infact, you can remove the square brackets completely. Commented May 22, 2014 at 10:43
  • that didn't work, same errors Commented May 22, 2014 at 10:55
  • Seems isql may not exist in that folder. Check me answer update. Commented May 22, 2014 at 11:02

4 Answers 4

1

There is a particular Popen argument for that: cwd, as mentioned here. Provide your command using an array and then the cwd parameter with where the command must be executed:

subprocess.Popen(['ls', '-l'], cwd="/path/to/folder")
Sign up to request clarification or add additional context in comments.

4 Comments

Yes. Most of the other answers here missed that you're passing your command as a string without using shell=True, which won't work properly. When you're using shell=False, you have to pass your command and its arguments as a list.
Also, if you need some help splitting your command up properly into a list, you can use the shlex.split() function: docs.python.org/2/library/shlex.html#shlex.split
Like @dano mentioned, you can give the command as you would in a shell with the option shell=True (though the documentation says this is unsafe). Other thing that could work to use a dedicated library like python-sybase.sourceforge.net/examples.html
@Disturbed_Entity If you run your command using only subprocess.Popen(['./isql']) (none of the args) does it work?
0

Popen only takes one args argument, for the command to run. You could try calling a shell with both the cd and isql commands as arguments, but changing the working directory from python is probably simpler

For the former approach:

subprocess.Popen('/bin/sh -c "cd /path/to/... && isql -arg1..'...)

for the latter:

os.chdir('/path/to...')
subprocess.Popen('isql -arg1..'...)

1 Comment

how do i pass this as an argument? ['isql', '-S server' -U %s -P %s -D %s <<EOF SELECT * FROM UniqueIDRange EOF' % (server,user,passwd,database)]
0

Try:

import os
import subprocess

os.chdir('/path/to/sybase/bin')

if os.path.exists('isql') or os.path.exists(os.path.join('/path/to/sybase/bin', 'isql')):
    ps = subprocess.Popen('isql -S %s -U %s -P %s -D %s <<EOF SELECT * FROM sometable EOF' % (server,user,passwd,database), stdout=subprocess.PIPE, shell=True)
    out, err = ps.communicate()
else:
    print "isql does not exists in this folder"

I am not super experienced with subprocess but this is how I generally use it on the odd occasion. Hopefully someone else can give a better answer/explanation.

Edit: removed the square brackets to remove confusion.

Comments

0

i know it's been long but just wanted to close this question

from subprocess import Popen, PIPE
from textwrap import dedent

isql = Popen(['./isql', '-I', '/app/sybase/...',
              '-S', mdbserver,
              '-U', muserid,
              '-P', password, ...,
              '-w', '99999'], stdin=PIPE, stdout=PIPE, cwd=sybase_path)
output = isql.communicate(dedent("""\
    SET NOCOUNT ON
    {}
    go
""".format(User_Query)))[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.