RSQLite types issue

107 views Asked by At

This is 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?

0

There are 0 answers