我想从python执行以下命令。当我从shell运行它时,我得到所需的输出,但用子进程调用它,我得到一个错误。作为python子进程的mysql命令
的命令是:
to_date=`date +%Y-%m-%d`; mysql -uroot -p**** lportal -e "select COUNT(*) from User_ where loginDate like \"$to_date%\";" | sed 1d
这是我的Python代码:
from subprocess import *
cmd=''' to_date=`date +%Y-%m-%d`; mysql -uroot -p**** lportal -e "select COUNT(*) from User_ where loginDate like \"$to_date%\";" | sed 1d '''
cmd_out=Popen(cmd,stdout=PIPE,stdin=PIPE,shell=True,stderr=PIPE).communicate()
print cmd_out
这是我的错误,我知道这是\”有关,但没有弄清楚如何解决呢
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-04-17%' at line 1
你为什么要这样做,而不是使用mysql库直接连接? –