如何在带有PostgreSQL的tryCatch中使用dbGetQuery?
发布时间:2020-12-13 15:55:11 所属栏目:百科 来源:网络整理
导读:我尝试使用tryCatch从R查询我的PostgreSQL数据库. 基本上查询工作,但我无法设法捕获错误并对它们作出反应.这是一个例子 insert_rows - function(dframe,con){out - list()for(i in 1:nrow(dframe)){query - .... some insert statementtryCatch({dbGetQuery(
我尝试使用tryCatch从R查询我的PostgreSQL数据库.
基本上查询工作,但我无法设法捕获错误并对它们作出反应.这是一个例子 insert_rows <- function(dframe,con){ out <- list() for(i in 1:nrow(dframe)){ query <- .... some insert statement tryCatch({dbGetQuery(con,query)},error=function(e) print("caught")) } } 当我创建错误时,例如通过输入重复记录到唯一的PK,我确实看到了PostgreSQL错误和警告,但它是来自RPostgreSQL的标准打印输出.我在其他环境中使用过tryCatch,它总是以这种方式工作.我已经使用了dbGetQuery,但我不能让它们一起工作.此外,将tryCatch放入列表中确实有很大帮助. 解决方法
使用dbSendQuery发送insert语句.在这种情况下,tryCatch将捕获异常:
tryCatch({ dbSendQuery(con,"insert into wrongtable values(1,2,3)") },error = function(e) print(e) ) 对于使用dbGetQuery(我不知道它失败的原因),有一个解决方法 – tryCatch({ res <- postgresqlExecStatement(con,"select * from thereisnotablewiththisname") postgresqlFetch(res) },error = function(e) print(e),warning = function(w) print(w) ) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |