[R] looping over string of frames when importing with 'sqlFetch' from a Microsoft Access database

Eric Fail eric.fail at gmx.us
Sat Feb 25 00:19:51 CET 2012


Dear R-list,

I am trying to import (all) frames from a Microsoft Access database as individual data frames in a fancy loop, but I'm having troubles figuring out how to use the 'sqlFetch' from the RODBS package in a loop (mostly because I can't figure out how to loop over elements (I came from stata)

I would very much appreciate if anyone on the list could help me solve this problem, as it is an issue of connecting to a database I can't really make a working example, please bear with me.

### not-working R code ###

## first I establish a connection to my database
mdbConnect<-odbcConnectAccess("C:\\... \\database.mdb")

## then I read of all the table names
stringTables <- sqlTables(mdbConnect, tableType=c("TABLE"))$TABLE_NAME

## and then I meet the wall ...
for(i.Frame in stringTables) {
    i.Frame <- sqlFetch(mdbConnect, i.Frame)
}
## this broken loop creates one data frame called containing the 'i.Frame' containing the last frame in the 'stringTables.' I'm not doing this correct.

## the final step.
DF <- stringTables[[1]]
for ( .df in stringTables) {
  DF <-merge(DF,.df, by.x="uniqueid", by.y="uniqueid", all=T)
 }

### end of not-working R code ###

Thanks,
Eric



More information about the R-help mailing list