尝试使用
RSQLite
过滤日期列,但似乎不起作用。哪里出了问题?
library(RSQLite)
library(tidyverse)
library(lubridate)
test_db <- dbConnect(RSQLite::SQLite(), "", extended_types = TRUE)
df <- tibble(date_col = ymd(c("2018-01-01", "2019-01-01")))
dbWriteTable(test_db, "df", df)
# Column is date
dbSendQuery(test_db, "PRAGMA table_info(df)") %>% dbFetch()
dbSendQuery(test_db, "SELECT * FROM df WHERE date_col = '2019-01-01'") %>% dbFetch()
dbSendQuery(test_db, "SELECT * FROM df WHERE date_col = date('2019-01-01')") %>% dbFetch()
由于 RSQLite 没有日期数据类型,因此请避免在 tibble 中使用 R 日期:
df <- tibble(date_col = c("2018-01-01", "2019-01-01"))
dbWriteTable(test_db, "df", df)
dbSendQuery(test_db, "SELECT * FROM df WHERE date_col = '2019-01-01'") %>% dbFetch()
#----results-----
date_col
1 2019-01-01