我知道如何在python中执行mysql脚本文件,但尝试传递参数并失败。
mysql -e
filename = 'test.sql'
cmd = ["mysql", "-h", 'local', "-u", 'user', "-p",'123' ,'-e','set @x=333']
process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output,err=process.communicate(str.encode("source "+filename))
test.sql
insert into dm.test(a) values(@x);
需要帮助!
我试了几次后自己回答,正确的方法是这样的:
filename = '/bigdata/test.sql'
cmd = ["mysql", "-h", '', "-u", '', "-p",'']
process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
outs,errs=process.communicate(str.encode("set @x=1;source "+filename+";"))
文件名的路径sep必须是unix风格的'/',在windows上运行时更改为这种风格。