I'd like to execute some Mysql commands from Linux, using Python and its subprocess module.
Without Python, from a shell, my command line is:
mysql --database=mydb --host=localhost --port=3306 --password= --execute="select * from mytable" --batch
With Python, I have:
cmd = ['mysql']
cmd.extend(['--database=', self._database])
cmd.extend(['--password=', self._password])
cmd.extend(['--execute=', query])
(...)
p = subprocess.Popen(cmd, shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
out, err = p.communicate()
errcode = p.returncode
Unfortunately, it doesn't work (mysql just print usage) and I think, subprocess generate this kind of output (' '.join(cmd)):
mysql --database= mydb --host= localhost --port= 3306 --password= --execute= "select * from mytable" --batch
ie. spaces are added between each parameters, separating = and value.
When I remove = in every parameters (cmd.extend(['--password', self._password])), it works fine, except when a parameter is void (so, I test if each parameter is void, then remove it if needed).
Finally, I found a workaround, by testing void parameters, but for future reference, is there any subprocess tip or usage I don't know to handle this kind of parameter= ? There must be some application when you have to use =, and I try to avoid Shell=True.