views:

89

answers:

3

This procedure works from the MySQL commandline both remotely and on localhost and it works when called from PHP. In all cases the grants are adequate:

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

I'm trying to call it from 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)

The bare query runs fine. The procedure call fails with

RApache Warning/Error!!!Error in mysqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not run statement: PROCEDURE myDB.lee_expout can't return a result set in the given context)

The MySQL docs say

For statements that can be determined only at runtime to return a result set, a PROCEDURE %s can't return a result set in the given context error occurs.

One would think that if a procedure were going to throw that error, it would be thrown under all circumstances instead of just from R.

Any thoughts on how to fix this?

A: 

Don't now about R, but this

p <- paste('CALL lee_expout(', exp_id, ',', group_id,')', sep= ' ') 

does look a bit ugly, ie like string concatenation. Maybe R's database driver takes that badly. In general, you can use placeholders for variables and pass the values on as separate arguments. Besides various security arguments, this also takes care of any type/apostrophe/whatever issues - maybe here, too?

Nicolas78
yeah, I know it's ugly. But I haven't found a better way in R; believe me I was looking for placeholders! In any event, I've echo'd the strings and they are correct. And the bare query is created via the same `paste` method. So I don't think it is a string problem. You're right that it's a good place to begin debugging though.
dnagirl
It is string concatenation, but used more often in R. You don't have to add the sep=' ' by the way, space is the default separator.
Joris Meys
+1  A: 

As far as I know, calling SQL procedures from R (dbCallProc) is not yet formally implemented (see reference manual of 24 july 2010 : http://cran.r-project.org/web/packages/RMySQL/RMySQL.pdf)

RMySQL is transferred from S3 to S4 programming style, and is currently still under development (version 0.7 being the current one). I suggest you ask the same question on the database mailing list for R :

https://stat.ethz.ch/mailman/listinfo/r-sig-db

If it is possible, they'll show you how. If it isn't, they'll tell you why.

Joris Meys
I did see that `dbCallProc` wasn't yet implemented. That's why I tried a straight query, figuring that what works in other languages might work in R too. Thanks for the link to the mailing list. I will definitely try there and report back.
dnagirl