问题
I'm trying to run this command through KennethReitz's Envoy package:
$ sqlite3 foo.db 'select * from sqlite_master'
I've tried this:
r = envoy.run("sqlite3 foo.db 'select * from sqlite_master'")
sqlite3: Error: too many options: "*"
and this:
r = envoy.run(['sqlite3', 'foo.db', 'select * from sqlite_master'])
AttributeError: 'NoneType' object has no attribute 'returncode'
additional quoting & escaping doesn't seem to help. Any suggestions?
FYI: This is what I had to do for now:
cmd = "sqlite3 %(database)s 'select * from sqlite_master'" % locals()
os.system(cmd)
Note that this is a contrived example, and that most of the unix shell commands that I'd like to issue aren't just a simple select that could be easily done via SQLAlchemy.
回答1:
You could use subprocess
:
from subprocess import check_output as qx
output = qx(['sqlite3', 'foo.db', 'select * from sqlite_master'])
print output
Or sqlite3
module:
import sqlite3
conn = sqlite3.connect('foo.db')
for row in conn.execute('select * from sqlite_master'):
print row
If you still want to use envoy
then you could fix it as:
import envoy
r = envoy.run([["sqlite3", "foo.db", "select * from sqlite_master"]])
print r.std_out
回答2:
This will not work in envoy
because envoy splits the commands and pass them to subprocess. Even if you try with subprocess.Popen(command, shell = False)
you will end up getting sqlite3
terminal. Both subprocess
and envoy
fails to address this, I will be happy if you can open an issue in envoy
since I am contributing to it, I will be thinking about this.
来源:https://stackoverflow.com/questions/9106350/how-to-use-wildcards-with-envoy