RSQLite - dbWriteTable - field.type - 如何获得正确的日期格式?
RSQLite - dbWriteTable - field.type - How to get proper Date format?
RSQLite::dbWriteTable
function, properly pass Dates from df
to db.
我的 df 上有一些日期字段,当我尝试使用上述函数和设置 [=14= 在 SQLite 数据库上编写 table 时] 它将这些日期字段写为数字而不是日期,例如:
"2018-12-01"
becomes 17866
这是我的完整代码:
dbWriteTable(db, "table", df, overwrite = T, append = F,
field.types = c(Col1 = "Date", Col2 = "Date"))
这实际上是在 SQLite
db
上写入 "table"
并为此类列使用正确的 Date
格式,但单元格内的值不是日期,它们仍然存在17866
.
等数字
Here 我发现有人建议解决方法,转换日期 as.character。
Is there a proper way to pass a date value to an SQLite db?
提前致谢
现在回顾一下,我从来没有真正找到一种方法来按照我的问题中的描述来做到这一点。最后,我采用了我在问题中提到的解决方法:
transform all the date variables in text before writing on the SQLite
DB.
“extended_types”参数可能适用于 DBconnection。
db <- DBI::dbConnect(RSQLite::SQLite(),
dbname = "xxx",
extended_types = T) # this point
RSQLite::dbWriteTable
function, properly pass Dates fromdf
to db.
我的 df 上有一些日期字段,当我尝试使用上述函数和设置 [=14= 在 SQLite 数据库上编写 table 时] 它将这些日期字段写为数字而不是日期,例如:
"2018-12-01"
becomes17866
这是我的完整代码:
dbWriteTable(db, "table", df, overwrite = T, append = F,
field.types = c(Col1 = "Date", Col2 = "Date"))
这实际上是在 SQLite
db
上写入 "table"
并为此类列使用正确的 Date
格式,但单元格内的值不是日期,它们仍然存在17866
.
Here 我发现有人建议解决方法,转换日期 as.character。
Is there a proper way to pass a date value to an SQLite db?
提前致谢
现在回顾一下,我从来没有真正找到一种方法来按照我的问题中的描述来做到这一点。最后,我采用了我在问题中提到的解决方法:
transform all the date variables in text before writing on the SQLite DB.
“extended_types”参数可能适用于 DBconnection。
db <- DBI::dbConnect(RSQLite::SQLite(),
dbname = "xxx",
extended_types = T) # this point