mardi 29 septembre 2015

Iterate over SQLite results from R

I'm new using R as language programming. And now I'm triying a basic loop over the results of a query, but I can't do it properly, let's see:

I have a DB named RDB.sqlite with a table named PERDIDAS, with the following structure


| ID | PERIODO | VALOR |

I have the following R code:

library("RSQLite")

con <- dbConnect(SQLite(), dbname = "bd/RDB.sqlite")

periodo <- function(num){


    results<-dbGetQuery(con, paste("SELECT PERIODO,VALOR FROM PERDIDAS WHERE PERIODO = ",num,""))
    results<-fetch(results,-1) // fetch all results


    # WHEN I TRY ITERATE OVER THE RESULTS...

    for (result in results){
      print(result)
    }

}
results(2)

When I run this script, that simply calls the results function passing it 2 as parameter. That's what I see on console:

[1] 2 2 2 2

[1] 2219.398 2218.256 2209.780 2236.723

What is wrong ? I don't know how properly iterate over all elements.. could you help me? I expect to see something like that on console:

[1] 2219.398

[2] 2218.256

[3] 2209.780

[4] 2236.723

Aucun commentaire:

Enregistrer un commentaire