jeudi 11 juin 2015

RSQLite types issue

Not a question - a resolved issue that confused me a lot. Somewhat related to RSQLite typecasting issue.

Consider the following example:

> require(RSQLite)
> DB = dbConnect(RSQLite::SQLite(),":memory:")

> dbSendQuery(DB,"create table tbl (X1 INT, X2 INT)")
> dbSendQuery(DB,"insert into tbl values (1,1),(2,0.1)")

> all.DF <- dbGetQuery(DB,"select *, typeof(X2) from tbl")
> part.DF <- dbGetQuery(DB,"select *, typeof(x2) from tbl where X1 NOT IN (1)")

Calling

> part.DF

gives

X1  X2   typeof(x2)
2   0.1  real

but

> all.DF

gives

X1 X2 typeof(X2)
1  1  integer
2  0  real

You see the problem, right? In part.DF the second value is correctly 0.1, while in all.DF the second value is 0, although the type is correctly identified as "real"! This is VERY confusing - the type is correct, but somewhere along the way from SQLite to R value 0.1 was obviously turned into integer:

> str(all.DF)

'data.frame':   2 obs. of  3 variables:
 $ X1        : int  1 2
 $ X2        : int  1 0
 $ typeof(X2): chr  "integer" "real"

while in part.DF it was correctly kept as "real"

> str(part.DF)

'data.frame':   1 obs. of  3 variables:
$ X1        : int 2
$ X2        : num 0.1
$ typeof(x2): chr "real"

Why?

Aucun commentaire:

Enregistrer un commentaire