[R] Insert variable in RMySQL-Statement

Hugo Mildenberger Hugo.Mildenberger at web.de
Wed Feb 9 14:54:05 CET 2011


This should be much faster:

   idx <- c(1,2,3)
   res <- dbGetQuery(con,paste("select * from db where id in (",paste(idx,collapse=","),")",sep=""));
   print(res);


On Wednesday 09 February 2011 14:13:17 Hugo Mildenberger wrote:
> Arne,
> 
> there might be faster or more elegant solutions, but this is a possibility:
> 
> for ( a in values ) { 
>        Temp <- dbGetQuery(con, paste("select * from db where id = ",a,sep="")));
>        print(Temp);
> }
> 
> There must be also a way to make use of the "in" - operator: 
> 
>    "select * from db where id in (1,2,3);"
> 
> but I can't figure it out at the moment.
> 
> Best
> 
> Hugo
> 
> On Wednesday 09 February 2011 10:09:34 Arne H. Schulz wrote:
> > Dear list,
> > I'm working with a MySQL-Table and would like to extract selected ids that are stored in a vector:
> > Ids <- c(1,2,3)
> > 
> > How do I insert this variable in a for loop like this:
> > 
> > For (a in values) {
> > 	Temp <- dbGetQuery(con,
> > 		"select * from db
> > 			Where id = <ID>";)
> > }
> > 
> > My problem is to split the select-statement and insert the current id. How can I handle this problem?
> > 
> > Best regards,
> > Arne
> > 
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list