2

Shortly, I am using JDBC and I am trying to write a query that will return some values from an SQL developer db table.

So far i have this:

#Query for getting data
sql <- paste("select * 
           FROM GRID Z
           where Z.LAT = Xlat AND Z.LON = Xlon")
fun <- dbGetQuery(jdbcConnection, sql)
attach(fun)

Problem is that Xlat and Xlon are variables in R and their values change frequently so I can't really hard-pass them into the query. Apparently, Z.LAT and Z.LON correspond to GRID table.

Question is: Is it possible to use R variables into a query as such?

I would also like to know if instead of = is there something to match the closest or nearest values.

Appreciate any suggestions. Thanks

Edit: Another approach to this would be to SELECT * from my table, and then 'play' with fun in order to get my values. Any thoughts or practices on that?

Note: jdbcConnection is asking for a remote connection.

Steve Pettifer
  • 1,975
  • 1
  • 19
  • 34
  • The commenter who has now disappeared is right - Why tag this with SQL Server? Or, for that matter, oracle-sqldeveloper? There is no such thing as a 'SQL developer table': It is clearly a question about Oracle and JDBC so tag it as such and you'll get relevant answer much more quickly on the whole. It also helps anyone who comes after looking for the same sort of question. – Steve Pettifer May 09 '14 at 11:33
  • @StevePettifer its "sql developer db table". notice the db. And that is why people can edit other people questions, in order to help them do that. he just clicked and went on. that's not legit as far as i know –  May 09 '14 at 11:38
  • There's still no such thing as "sql developer db table" because SQL Developer is just and IDE not an RDBMS. I agree he could (and should) have edited or elucidated more but he didn't do anything 'wrong' as such. The more accurate the information you enter (including the version of the RDBMS in question), the better the answer tends to be (and quicker of course). – Steve Pettifer May 09 '14 at 12:00
  • @StevePettifer i hardly know the differences between the syntax of each and every rdbms and that is normal. i knew however there are some and that's why i precised the `sql dev db table`. "he didn't do anything 'wrong as such'" doesn't make sense. anyway thanks for your feedback –  May 09 '14 at 12:11

4 Answers4

2

Are you looking for this?

sql <- paste0("select * 
           FROM GRID Z
           where Z.LAT ='", Xlat,"' AND Z.LON = '", Xlon,"'")

I assumed that your variables are character. In case the above is running behind a web server, there are options for URL encode and escape to avoid code injections... like this

EDIT: About this:

I would also like to know if instead of = is there something to match the closest or nearest values.

Since you are executing your query via a SQL engine that is more a SQL question than a R one. Like @Vivek says you can do that in sqldf but I guess your data are in a remote database, so it wouldn't help in this case.

All SQL flavours have like, so just use it in your query. Please tell me if I'm misunderstanding your question.

sql <- paste0("select * 
           FROM GRID Z
           where Z.LAT like '", Xlat,"' AND Z.LON like '", Xlon,"'")
Community
  • 1
  • 1
Michele
  • 8,563
  • 6
  • 45
  • 72
  • yes i am asking for a remote connection. Why `sqldf` wouldn't work? Nope you got this right. Can you please edit your answer with `like`? I am not really familiar with `sql` syntax and all. thanks a ton –  May 09 '14 at 11:24
  • `sqldf` allows you to use `sql` syntax to `R` object. Which means that the object MUST be in the R environment, there is no connection to a remote DBMS. – Michele May 09 '14 at 11:27
  • @KapelNick Edited with `like` – Michele May 09 '14 at 11:39
  • it seems to work fine, however for some reason it doesn't return any values. –  May 09 '14 at 11:47
  • @KapelNick it means there is no data probably. The query syntax is correct, but I can tell about the meaningfulness of the query – Michele May 09 '14 at 11:49
2

gsubfn's fn$ operator supports a quasi-perl sort of string interpolation like this:

library(gsubfn)

sql0 <- "select * FROM GRID Z where Z.LAT = $Xlat AND Z.LON = $Xlon"
fun <- fn$dbGetQuery(jdbcConnection, sql0)

or like this to allow examination of sql after substitution:

sql0 <- "select * FROM GRID Z where Z.LAT = $Xlat AND Z.LON = $Xlon"
sql <- fn$identity(sql0)
fun <- dbGetQuery(jdbcConnection, sql)

See ?fn and also the examples on the sqldf home page.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
1

yes it is certainly, possible see library sqldf. you could use for approximate match Z.LAT like 'Xlat'

data(CO2)

var1<-"Plant"
var2<-"conc"

require(sqldf)
squery2<-paste("select * from CO2 where",var1,"like 'Qn%'","and",var2,"> 500",sep=" ")

> sqldf(squery2)
  Plant   Type  Treatment conc uptake
1   Qn1 Quebec nonchilled  675   39.2
2   Qn1 Quebec nonchilled 1000   39.7
3   Qn2 Quebec nonchilled  675   41.4
4   Qn2 Quebec nonchilled 1000   44.3
5   Qn3 Quebec nonchilled  675   43.9
6   Qn3 Quebec nonchilled 1000   45.5
Silence Dogood
  • 3,587
  • 1
  • 13
  • 17
  • according to @Michele `sqldf` needs to be used with something in local. I am using a remote database, so if it's true i cant really use it. i'll edit my question to reflect that. thanks –  May 09 '14 at 11:29
  • the `sqldf` demo was for illustration, my point was to show the construction of the query, which is same for local or remote database – Silence Dogood May 09 '14 at 11:45
-1

You can also use this - Suppose,

 Xlat <- 8
 Xlon <- 10

Then,

 #Query for getting data
 sql <- paste("select * 
       FROM GRID Z
       where Z.LAT = $Xlat AND Z.LON = $Xlon")
 fun <- dbGetQuery(jdbcConnection, sql)
 attach(fun)
RHelp
  • 815
  • 2
  • 8
  • 23
  • Are you sure that would work? It seems `PHP` syntax to me... – Michele May 09 '14 at 11:22
  • i am afraid that won't work. `$` is valid in R but Sql does not recognize it.. –  May 09 '14 at 11:25
  • The sqldf package doc says it works - http://code.google.com/p/sqldf/ See - Example 5. Insert Variables – RHelp May 09 '14 at 11:27
  • from [here](https://code.google.com/p/sqldf/#Example_5._Insert_Variables) `Here is an example of inserting evaluated variables into a query using gsubfn quasi-perl-style string interpolation. gsubfn is used by sqldf so its already loaded. Note that we must use the fn$ prefix to invoke the interpolation functionality:` This is a very very particular high level wrapper that, behind the scene, probably uses `paste`. However, it is about **sqldf**, it won't work in here – Michele May 09 '14 at 11:37
  • 1
    I tried `$` operator therefore i know as a fact it doesn't. –  May 09 '14 at 11:39
  • 1
    @RHelp the only way to do something similar in `R` (in reality) is using `sprintf`. E.g. `sprintf("%s %d", "test", 1:3)` – Michele May 09 '14 at 11:46