subprocess & isql
Frank Millman
frank at chagford.com
Fri Jul 15 09:52:29 EDT 2011
"peterff66" <peterff66 at yahoo.com> wrote in message
news:ivo9or+jpph at eGroups.com...
> Hello Python community,
>
> I am working on a project that retrieves data from remote Sybase. I have
> to use isql and subprocess to do this. Is the following correct?
>
> 1. call subprocess.popn to run isql, connect to Sybase
> 2. run sql ("select ...from ...")
> 3. write retrieved data to subprocess.pipe
> 4. retrieve data from pipe
>
> Did anybody do similar work before? Any ideas (or code piece) to share?
I did something vaguely similar a while ago. I don't know how helpful this
will be, but it may give you some ideas.
Firstly, these are the main differences between your scenario and mine -
1. I was using 'osql' to connect to MS SQL Server.
2. I used os.popen4, not subprocess. Hopefully you can follow the guidelines
in the manual to translate to subprocess.
3. In my case, I built up a long command to create and populate various
database tables using StringIO, passed the string to popen4.stdout, and then
read popen4.stdin and displayed it on the screen to read any output.
4. On looking at my code, I see that I started a sub-thread to read
popen4.stdin. I can't remember why I did that, but it may have been
something to do with getting the output in realtime instead of waiting for
the command to finish executing.
Here is the code that I used -
import os
import threading
from cStringIO import StringIO
def read_stdout(stdout):
while True:
line = stdout.readline()
if not line:
break
print line
os.environ['OSQLPASSWORD'] = pwd
sql_stdin, sql_stdout = os.popen4('osql -U %s -d %s -n' % (user,
database))
s = StringIO()
[call function to build up commands]
threading.Thread(target=read_stdout, args=(sql_stdout,)).start()
s.seek(0)
sql_stdin.writelines(s.readlines())
s.close()
sql_stdin.close()
HTH
Frank Millman
More information about the Python-list
mailing list