我想從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庫直接連接? –