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?

Bad_Coder
  • 764
  • 1
  • 16
  • 34

4 Answers4

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")
Community
  • 1
  • 1
marcelocra
  • 1,614
  • 2
  • 21
  • 32
  • 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. – dano May 22 '14 at 14:21
  • Also, if you need some help splitting your command up properly into a list, you can use the `shlex.split()` function: https://docs.python.org/2/library/shlex.html#shlex.split – dano May 22 '14 at 14:23
  • 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 http://python-sybase.sourceforge.net/examples.html – marcelocra May 22 '14 at 17:30
  • @Disturbed_Entity If you run your command using only `subprocess.Popen(['./isql'])` (none of the args) does it work? – dano May 22 '14 at 17:35
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..'...)
otus
  • 4,957
  • 29
  • 46
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.

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]
Bad_Coder
  • 764
  • 1
  • 16
  • 34