Search code examples
pythonpython-envoy

How to use wildcards with Envoy


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.


Solution

  • 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