用 R 在命令行中编写 mysql 的导出过程
Writing in command line an export procedure for mysql with R
我正在使用命令行从 mysql 导出查询结果:
SET sql=C:\Program Files\MySQL\MySQL Workbench CE 6.1.4\mysql.exe
SET p_s=C:\Rtools\bin\sed.exe
"%sql%" --skip-secure-auth --host=xxx.xxx.xx.x --user=myuser --password=mypass mydb <c:/Temp/my_query.txt | "%p_s%" 's/\t/;/g' > C:/Temp/myfile.txt
这对我来说非常有用。现在我想在 R 中编写这个过程。我正在尝试这样的事情:
b<-"SET sql=C:\Program Files\MySQL\MySQL Workbench CE 6.1.4\mysql.exe"
a<-"SET p_s=C:\Rtools\bin\sed.exe"
s<- ' "%sql%" --skip-secure-auth --host=xxx.xxx.xx.x --user=myuser--password=mypass mydb <c:/Temp/my_query.txt | "%p_s%" \'s/\t/;/g\' > C:/Temp/myfile.txt'
system(a)
system(b)
system(s)
但这不起作用。如果我 运行:
try(system(a, intern = TRUE))
它returns:Error in system(a, intern = TRUE) : 'SET' not found
有什么建议吗?
我按照@hrbrmstr 的建议解决了创建 .BAT 文件的问题
p1<-"SET sql=C:\Program Files\MySQL\MySQL Workbench CE 6.1.4\mysql.exe"
p2<-"SET p_s=C:\Rtools\bin\sed.exe"
host<-"xxx.xxx.xx.x"
user<-"myuser"
password<-"mypass"
db<-"mydb"
sep<-";"
file<-"C:/Temp/myfile.txt"
query<- "SELECT * FROM mytable limit 100;"
cmd<-paste('"%sql%" --skip-secure-auth --host=',host,' --user=',user,' --password=',password,' ',db,' <c:/Temp/query.sql | "%p_s%" \'s/\t/',sep , '/g\' >', file,sep="")
cmd<-paste(p1,p2,cmd, sep="\n")
setwd("c:\temp")
write(query,"query.sql")
write(cmd,"mycommand.bat")
system("mycommand.bat")
我正在使用命令行从 mysql 导出查询结果:
SET sql=C:\Program Files\MySQL\MySQL Workbench CE 6.1.4\mysql.exe
SET p_s=C:\Rtools\bin\sed.exe
"%sql%" --skip-secure-auth --host=xxx.xxx.xx.x --user=myuser --password=mypass mydb <c:/Temp/my_query.txt | "%p_s%" 's/\t/;/g' > C:/Temp/myfile.txt
这对我来说非常有用。现在我想在 R 中编写这个过程。我正在尝试这样的事情:
b<-"SET sql=C:\Program Files\MySQL\MySQL Workbench CE 6.1.4\mysql.exe"
a<-"SET p_s=C:\Rtools\bin\sed.exe"
s<- ' "%sql%" --skip-secure-auth --host=xxx.xxx.xx.x --user=myuser--password=mypass mydb <c:/Temp/my_query.txt | "%p_s%" \'s/\t/;/g\' > C:/Temp/myfile.txt'
system(a)
system(b)
system(s)
但这不起作用。如果我 运行:
try(system(a, intern = TRUE))
它returns:Error in system(a, intern = TRUE) : 'SET' not found
有什么建议吗?
我按照@hrbrmstr 的建议解决了创建 .BAT 文件的问题
p1<-"SET sql=C:\Program Files\MySQL\MySQL Workbench CE 6.1.4\mysql.exe"
p2<-"SET p_s=C:\Rtools\bin\sed.exe"
host<-"xxx.xxx.xx.x"
user<-"myuser"
password<-"mypass"
db<-"mydb"
sep<-";"
file<-"C:/Temp/myfile.txt"
query<- "SELECT * FROM mytable limit 100;"
cmd<-paste('"%sql%" --skip-secure-auth --host=',host,' --user=',user,' --password=',password,' ',db,' <c:/Temp/query.sql | "%p_s%" \'s/\t/',sep , '/g\' >', file,sep="")
cmd<-paste(p1,p2,cmd, sep="\n")
setwd("c:\temp")
write(query,"query.sql")
write(cmd,"mycommand.bat")
system("mycommand.bat")