I have a question related to MySQL and Python.
This command works on the shell, but not when I use os.execvp
.
$./mysql -D test -e "show tables" +----------------+ | Tables_in_test | +----------------+ | sample | +----------------+
The corresponding piece of code in python would be
def execute():
args = []
args.extend(sys.argv[1:])
args.extend([MYSQL, '-D test -e "show tables"'])
print args
os.execvp(args[0], args)
child_pid = os.fork()
if child_pid == 0:
os.execvp(args[0], args)
else:
os.wait()
The output of this is:
[./mysql', '-D test -e "show tables"'] ERROR 1049 (42000): Unknown database ' test -e "show tables"'
I am not sure if this is a problem with the Python syntax or not. Also, the same command works with an os.system
call.
os.system(MYSQL + ' -D test -e "show tables"')
Please let me know how to get this working.
Try:
You might also be interested in the
subprocess
module if you weren't aware of it:Or just
subp.call([MYSQL, ...])
and you don't have to fork+exec yourself, exit status is the return value IIRC.