如何使用 RSQLite 编写带有日期的 SQL 查询。下面是一个例子。 dbGetQuery
不返回任何行。
require(RSQLite)
require(ggplot2)
data(presidential)
m <- dbDriver("SQLite")
tmpfile <- tempfile('presidential', fileext='.db')
conn <- dbConnect(m, dbname=tmpfile)
dbWriteTable(conn, "presidential", presidential)
dbGetQuery(conn, "SELECT * FROM presidential WHERE Date(start) >= Date('1980-01-01')")
最佳答案
只是为了说明,这很好用:
tmpfile <- tempfile('presidential', fileext='.db')
conn <- dbConnect(m, dbname=tmpfile)
p <- presidential
p$start <- as.character(p$start)
p$end <- as.character(p$end)
dbWriteTable(conn, "presidential", p)
dbGetQuery(conn, "SELECT * FROM presidential WHERE start >= '1980-01-01'")
您可以在文档 here 中阅读有关 SQLite 中缺少 native 日期类型的信息。 .我在 SQLite 中使用字符串作为日期已经很长时间了,以至于我实际上完全忘记了这个问题。
是的,我编写了一个小的 R 函数,可以将数据框中的任何日期列转换为字符。对于简单的比较,将它们保存在
YYYY-MM-DD
就足够了,如果我需要做算术,我会在 R 中转换它们。
关于r - 在 RSQLite 中使用日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13462086/