Question

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.

Was it helpful?

Solution

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,"'")

OTHER TIPS

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.

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

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top