从R调用时,MySQL存储过程失败

dna*_*irl 9 mysql stored-procedures r rmysql

这个过程可以从MySQL命令行远程和本地主机上运行,​​并且在从PHP调用时可以正常工作.在所有情况下,补助金都是足够的:

CREATE PROCEDURE `myDB`.`lee_expout` (IN e int, IN g int)
BEGIN

select lm.groupname, lee.location, starttime, dark,
  inadist,smldist,lardist,emptydur,inadur,smldur,lardur,emptyct,entct,inact,smlct,larct
from lee join leegroup_map lm using (location)
where exp_id= e and std_interval!=0 and groupset_id= g
order by starttime,groupname,location;

END
Run Code Online (Sandbox Code Playgroud)

我试图从R调用它:

library(DBI)
library(RMySQL)

db <- dbConnect(MySQL(), user="user", password="pswd",
        dbname="myDB", host="the.host.com")

#args to pass to the procedure
exp_id<-16
group_id<-2

#the procedure call
p <- paste('CALL lee_expout(', exp_id, ',', group_id,')', sep= ' ') 

#the bare query
q <- paste('select lm.groupname, lee.location, starttime, dark,
inadist,smldist,lardist,emptydur,inadur,smldur,lardur,emptyct,entct,inact,smlct,larct
from lee join leegroup_map lm using (location)
where exp_id=', 
exp_id, 
' and std_interval!=0 and groupset_id=', 
group_id, 
'order by starttime,groupname,location', sep=' ') 

rs_p <- dbSendQuery(db, statement=p) #run procedure and fail
p_data<-fetch(rs_p,n=30)

rs_q <- dbSendQuery(db, statement=q) #or comment out p, run query and succeed
q_data<-fetch(rs_q,n=30)
Run Code Online (Sandbox Code Playgroud)

裸查询运行正常.过程调用失败

RApache警告/错误!!! mysqlExecStatement(conn,statement,...)出错:RS-DBI驱动程序:(无法运行语句:PROCEDURE myDB.lee_expout无法返回给定上下文中的结果集)

MySQL的文档

对于只能在运行时确定以返回结果集的语句,PROCEDURE%s无法在给定的上下文错误中返回结果集.

有人会认为,如果一个程序要抛出该错误,它将在所有情况下抛出,而不仅仅是从R抛出.

有关如何解决此问题的任何想法?

Nic*_*s78 1

现在不谈 R,但这个

p <- paste('CALL lee_expout(', exp_id, ',', group_id,')', sep= ' ') 
Run Code Online (Sandbox Code Playgroud)

看起来确实有点难看,即像字符串连接。也许 R 的数据库驱动程序对此很不满意。一般来说,您可以使用变量的占位符并将值作为单独的参数传递。除了各种安全参数之外,这还可以解决任何类型/撇号/任何问题 - 也许也在这里?

  • 它是字符串连接,但在 R 中使用得更频繁。顺便说一句,您不必添加 sep=' ',空格是默认分隔符。 (2认同)