[R] Functions Accessing Databases

bartjoosen bartjoosen at hotmail.com
Fri Apr 3 12:16:26 CEST 2009


Hi,

There seems to be something wrong with your function getinfo.

Try first to substitute your dbGetQuery by a print statement, so that you
can see what is going on:
getinfo=function(t){print(the SQL query)}
allinfo=sapply(c(1985:2007),getinfo)

If these results look good, you can change your function back to
dbGetQuery(con, ....)
If not, show your results, and we will look again

Bart




Bob Roberts-2 wrote:
> 
> Hello,
>    I'm accessing a MySQL database from inside R and had a problem with a
> function. In the database, there is data from years 1985 to 2007 that I
> would like to retrieve. I can easily get the data from one year by the
> following:
> info1985=dbGetQuery(con, statement='the SQL query')
> Inside the statement, I have a column that is set to the desired year
> (e.g. table.column=1985) through a WHERE clause, but when I write a
> function like the following:
> getinfo=function(t){dbGetQuery(con,statement='the SQL query')}
> and instead of using a specific year, I substitute in t (e.g.
> table.column=t) in a WHERE clause
> When I go to do
> allinfo=sapply(c(1985:2007),getinfo)
> I get the following error message: RS-DBI driver: (could not run
> statement: Unknown column 't' in 'where clause')
> Is there anyway around this error? Or is not possible to substitute in
> years with a function and sapply through SQL?
> Thanks so much.
> 
> 
> 
>       
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Functions-Accessing-Databases-tp22860193p22865610.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list