- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何在 dbGetQuery()
的 bind.data
参数中为像
select * from tst where x = ? and y in (?)
这是我尝试过的:
> library("RSQLite")
> c <- dbConnect (SQLite())
> dbGetQuery(c, "create table tst (x int, y int)")
> dbGetQuery(c, "insert into tst values (?, ?)", data.frame(x=c (1,2,1,2), y=c(3, 4, 5, 6)))
> dbReadTable(c, "tst")
x y
1 1 3
2 2 4
3 1 5
4 2 6
> dbGetQuery (c, "select * from tst where x = ? and y not in (?)", data.frame(x=2, y=I (list(7,6))))
Error in sqliteFetch(rs, n = -1, ...) :
RAW() can only be applied to a 'raw', not a 'double'
从阅读源代码来看,任何非 data.frame bind.data
参数都被强制通过 as.data.frame()
,所以我想这没什么意义尝试除数据框以外的任何东西。
注意:哎呀,似乎即使绑定(bind)一个集合也是有问题的:
> dbGetQuery(c, "select * from tst where y not in (?)", c(7,6))
x y
1 1 3
2 2 4
3 1 5
4 2 6
5 1 3
6 2 4
7 1 5
这清楚地表明从 R 发送了 2 个单独的查询(其中一个返回 4 个结果,另一个返回 3 个结果); SQLite 永远不会看到设置参数。
早期说明:我希望数据库引擎过滤适当的行,我不希望 R 计算笛卡尔积。在上面的示例中,简单地去掉 I()
会创建一个 2 行的数据框(感谢 R 的回收),其中之一就是解决方案。 R 将这两行中的每一行发送到 sqlite,当然第二行匹配。但是下面显示了 SQLite 引擎实际上并没有接收到带有常规 data.frames 的设置参数:
> dbGetQuery(c, "select * from tst where x in (?) and y in (?)", data.frame(x=c(3,2), y=c(6,7)))
[1] x y
<0 rows> (or 0-length row.names)
> dbGetQuery(c, "select * from tst where x in (?) and y in (?)", data.frame(x=c(3,2), y=c(7,6)))
x y
1 2 6
最佳答案
为什么指定 y = I(list(7,6))
而不是 y=c(6,7)
?这似乎有效:
dbGetQuery (c,
"select * from tst where x = ? and y in (?)",
data.frame(x=1, y=c(7,6)))
您可能正在寻找 expand.grid
。
dbGetQuery (c,
"select * from tst where x = ? and y in (?)",
expand.grid(x=c(2,3), y=c(7,6)))
编辑:另一种选择(并不漂亮)是替换 R
中的 ?
。类似于以下内容:
dbGetQuerySet <- function(con, statement, ...){
if (length(list(...)) > 0){
bind.data <- list(...)[[1]]
for (set in as.data.frame(bind.data)){
statement <- sub('\\?', paste(set, collapse=","), statement)
}
}
sqliteQuickSQL(con, statement, ...)
}
关于sql - RSQLite:在同一选择查询中绑定(bind)集和标量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25263909/
我正在查看 documentation of RSQLite .我创建了一个数据库并使用 推送了一些数据 mydb = 150) dbSendQuery(mydb, paste("UPDATE mtc
我刚刚开始涉足 sqlite 的世界,主要是通过 R 的接口(interface)包“RSQLite”。 但是,似乎在处理 sqlite 数据库时让计算机进入休眠状态使其锁定,然后尝试关闭连接不会做任
我完全有可能忽略了一些东西,但是在使用 RSQLite 包时,是否仍然可以在 R 脚本中利用特殊字符串“:memory:”来加速数据检索? 最佳答案 是的,仍然可以使用特殊字符串 :memory: .
我经常使用具有 >10,000 列的大型 CSV 文件 (>50GB)。我正在考虑将信息读入 RSQLite这样我就可以轻松进行查询和子集化。 问题 SQLite 在单个 SQL 中有 2000 个字
我正在尝试使用 RSQLite 从我的数据库中读取表格。所有表的列名都带有“.”。 例如:我的测试表有2列:index, first.name 如何编写查询来过滤带有名字列的测试表: 我的代码是: d
我有一个包含 x、y 和 z 列的 sqlite 表。 x 和 y 是唯一键,z 是值。 我想用 R 向这个表中插入数据。如果插入重复记录 - 基于 x 和 y 字段,我希望 sqlite 拒绝该记录
我一直找不到关于 RSQLite 如何处理因子的文档。从快速测试(见下文)来看,它们似乎已转换为字符。 问题一:有没有办法把它们作为因子保存下来?我可以想到一些笨拙的方法(主要涉及一个单独的表或存储因
如何使用 RSQLite 编写带有日期的 SQL 查询。下面是一个例子。 dbGetQuery不返回任何行。 require(RSQLite) require(ggplot2) data(presid
我正在尝试使用 RSQLite 包(我也尝试过 sqldf 包)将一个大的 csv 文件加载到 SQL lite 数据库中。该文件包含所有英国邮政编码和它们的各种查找值。 我想避免将其加载到 R 中,
在使用 RSQLite 1.0.0 包从 SQLite 3 数据库中提取数据时,我遇到了一个奇怪的现象。除日期时间值的年份外,所有内容都被截断! 看一个例子: 下面我正在提取 DATETIME 列:
我有一个 SQLite 数据库,我使用 R 的 RSQLite 包进行查询。我有一个分类向量,我想通过它来过滤我的查询,这样我的查询看起来像这样: dbGetQuery(mydb,
所以我试图访问我的数据库文件但没有成功。这是我的脚本: library(DBI) library(sqldf) drv <- dbDriver("SQLite") con <- dbConnect(d
使用 RSQLite 构建 SQLite 数据库我想一次发送多个语句 - 这可能吗? 为什么这些不起作用: sql <- readLines("createtables.sql") dbSendQue
我在 R 中使用 RSQLITE 来构建数据库并执行“JOIN”操作并使用以下方法提取行: res<-dbSendQuery(con," SELECT DISTINCT all_transcr
我已经开始使用 RSQLite 和 dplyr 来有效地处理大型数据集。但是,我一直无法协调如何让 RSQLite 格式化日期或这里有哪些最佳实践。下面的例子应该说明这个过程对我来说在哪里失败: li
我有一个包含各种 token 和频率计数的数据库。当我尝试使用 RSQLite 从 R 中检索这些时,我得到的负数很低,因此可能存在缓冲区溢出问题。如果我在 say sqliteman 中打开数据库并
如何在 dbGetQuery() 的 bind.data 参数中为像 这样的 SQL 语句传递标量和一组值 select * from tst where x = ? and y in (?) 这是我
在 SQLite 中,我想找到我用 GROUP BY 定义的(记录的)系列的一阶差分的标准偏差.我的数据提供者给了我一个每日价格序列,但我想找到年化的每日波动率(每日返回的标准差——该系列的自然对数的
使用RSQLite在 RStudio 中运行大型查询时遇到以下错误 Error in rsqlite_send_query(conn@ptr, statement) : database or
我正在构建一个供内部使用的包,并尝试从用户那里抽象出所有可能的数据库交互。我需要在函数内连接到数据库并断开与数据库的连接(我认为)。但是,断开连接不起作用。 `my_func = function()
我是一名优秀的程序员,十分优秀!