【发布时间】:2020-05-18 23:42:00
【问题描述】:
我在下面提到了 R 中的数据框。
ID Amount Date
IK-1 100 2020-01-01
IK-2 110 2020-01-02
IK-3 120 2020-01-03
IK-4 109 2020-01-03
IK-5 104 2020-01-03
我正在使用ID 使用以下代码从 MySQL 获取一些详细信息。
library(RMySQL)
conn<- connection
query<-paste0("SELECT c.ID,e.Parameters, d.status
FROM Table1 c
left outer join Table2 d ON d.seq_id=c.ID
LEFT outer JOIN Table3 e ON e.role_id=d.role
where c.ID IN (", paste(shQuote(dataframe$ID, type = "sh"),
collapse = ', '),")
and e.Parameters in
('Section1',
'Section2','Section3',
'Section4');")
res1 <- dbGetQuery(conn,query)
res2<-res1[res1$Parameters=="Section1",4:5]
colnames(res2)[colnames(res2)=="status"] <- "Section1_Status"
上面的代码工作正常,如果我传递了 ~1000 个 ID,但一次传递 10000 个或更多 ID 时会引发 R 终止错误。
如何创建一个循环并批量传递 ID 以获得 10000 ID 的最终输出。
错误信息:
Warning message:
In dbFetch(rs, n = n, ...) : error while fetching rows
【问题讨论】:
-
只是猜测,我们也可以不加入 r 数据帧吗?用
JOIN dataframe[, "ID", drop = FALSE] x ON x.ID = e.role_id替换你的“where ...”? -
@zx8754:我试过了,没用。
-
@Vector JX 能否请您包含错误消息。
-
@A.Suliman:运行大约 10 分钟后,R 在会话到期时终止,没有显示任何错误消息。传递大约 1000 个 ID 时,代码运行良好。
-
@A.Suliman:有时会收到类似
Warning message: In dbFetch(rs, n = n, ...) : error while fetching rows的错误消息