MySQL stored procedure fails when called from R
Asked Answered
D

3

9

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?

Draconian answered 30/8, 2010 at 20:10 Comment(3)
did you manage to run your store proc? can you mark correct answer which helps you? or if none of them you make post own and self-answer it. So the interested people can find a solution here. ThanksClaymore
@JanGorecki: I didn't manage to run the stored procedure. I had to use the bare query. This was a while ago though and perhaps R's DBI package is better about stored procedures now.Draconian
I don't use MySQL, but I have run SQL queries on Microsoft SQL databases using R. I noticed that whenever, in the query, there is anything but the bare select statement, the process fails. I don't know if it is necessary in MySQL, but have you tried removing the "create procedure", "begin" and "end" lines?Klute
R
3

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.

Rosina answered 30/8, 2010 at 21:45 Comment(1)
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.Draconian
F
3

Try adding:

client.flag=CLIENT_MULTI_STATEMENTS

to your connection parameters. It may help.

There are some details about this in the RMySQL PDF.

Fredericfrederica answered 2/11, 2010 at 8:46 Comment(0)
A
1

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?

Agonist answered 30/8, 2010 at 20:28 Comment(2)
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.Draconian
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.Rosina

© 2022 - 2024 — McMap. All rights reserved.