3

I'm using R to call a mySQL statement, where I define the variable outside the statement e.g.

foo = 23;
dbGetQuery(con, "select surname from names WHERE age = '.foo.' ;")

But this returns an empty set, I've googled around and tried'.&foo.' ".foo." '".&&foo."' and many different combinations, but none of them work, I think this should be a mysql question rather than an R specific problem I'm having, but not sure. Normally variables have $values but not in R.

2
  • Isn't dbGetQuery an R function? It looks like an R question, but you've got both tags. Commented Feb 2, 2010 at 7:06
  • Yes, you are right, I remove the mysql tag. I think the correct query is something like this, where the var goes a t the end: dbGetQuery(con, "select od from plate_data WHERE col = '%s'", foo) But this doesn't work either.. Commented Feb 2, 2010 at 7:21

5 Answers 5

8

This should work:

foo = 23;

sqlStatement <- paste("select surname from names WHERE age =",foo,'"',sep="")

dbGetQuery(con, sqlStatement;)
Sign up to request clarification or add additional context in comments.

3 Comments

I'd use paste("select surname from names WHERE age =",shQuote(foo),sep="")
thanks, this also works without the ' ' what are those for Robert? sqlStatement <- paste("select surname from names WHERE age =",foo,sep="")
@John. You're correct. The paste function already appends " for you so you don't need '"'. In some instances, when working with strings that have " in them, you can use single ' to wrap the string. This is very useful when dealing with the XML package where strings may have many nested ".
2

You may want to look at the answers to this question: Can I gracefully include formatted SQL strings in an R script?.

The simplest solution is to use the paste command as Robert suggested.

Comments

1

The accepted answer gives bad advice which leaves your application vulnerable to SQL injection. You should always use bind variables instead of concatenating values directly into your query. Use the dbGetPreparedQUery method as described in this answer: Bind variables in R DBI

Comments

0

Adding the semi-colon at the end of query sometimes creates problem. Try changing your query from:

dbGetQuery(con, "select surname from names WHERE age = '.foo.' ;")

to:

dbGetQuery(con, "select surname from names WHERE age = '.foo.'")

3 Comments

Thanks, but it still returns "data frame with 0 columns and 0 rows" where as if I put 23 instead of foo it works fine.
what do dot character mean '.foo.'?
I don't know what the dots mean, I was trying a few different things from other languages I found using different formats.
0

AFAIK the command has to be a string, so you should append the single components. Not being familiar with R I cant help you out HOW to do that. In MS-VBA the string concatenation operator is '&'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.