readwritesqlite icon indicating copy to clipboard operation
readwritesqlite copied to clipboard

rws_read dangerously converting values from text to integer

Open sebdalgarno opened this issue 3 years ago • 1 comments

I can create a table and set the type to be integer, but rws_write will still write a table containing a text column (i.e. no error at this stage). If I then read that data back, rws_read will coerce the values back to integer, although any text string will be converted to 0, which seems very dangerous.

library(readwritesqlite)
library(subfoldr2)
library(tibble)

age <- tibble(age = c(1L, "no age", NA_character_))

sbf_create_db("dbread-issue", ask = FALSE)

sbf_execute_db("CREATE TABLE age (
                age integer)")

con <- rws_connect(dbname = "output/dbs/dbread-issue.sqlite")

rws_write(age, conn = con)

x <- rws_read_table("age", conn = con)

x
> age
# A tibble: 3 × 1
  age   
  <chr> 
1 1     
2 no age
3 NA  
> x
# A tibble: 3 × 1
    age
  <int>
1     1
2     0
3    NA
> 

I'm not sure what the solution is. I didn't realize that the initial write wouldn't throw an error.

sebdalgarno avatar Sep 29 '22 00:09 sebdalgarno

there is a warning after running rws_read_table

Warning message:
In result_fetch(res@ptr, n = n) :
  Column `age`: mixed type, first seen values of type integer, coercing other values of type string

sebdalgarno avatar Sep 29 '22 00:09 sebdalgarno