使用 dbSendQuery 在数据库上创建 table 时避免出现警告消息“结果对象仍在使用中”

Avoiding warning message “There is a result object still in use” when using dbSendQuery to create table on database

背景:

我使用 dbplyrdplyr 从数据库中提取数据,然后我使用命令 dbSendQuery() 构建我的 table.


问题:

构建 table 后,如果我 运行 另一个命令,我会收到以下警告:

Warning messages: 1. In new_result(connection@ptr, statement): Cancelling previous query 2. In connection_release(conn@ptr) :
 There is a result object still in use. The connection will be automatically released when it is closed.


问题:

因为我没有要获取的结果(我正在发送构建 table 的命令),所以我不确定如何避免此警告。目前,我在构建 table 后断开连接,错误消失了。我能做些什么来避免这个警告吗?

目前一切正常,我只是收到这个警告。我只是想避免它,因为我认为我应该在构建 table.

之后清除 某些东西

代码示例

# establish connection con = DBI::dbConnect(<connection stuff here>)

# connect to table and database transactions = tbl(con,in_schema(“DATABASE_NAME”,”TABLE_NAME”))

# build query string query_string = “SELECT * FROM some_table”

# drop current version of table DBI::dbSendQuery(con,paste('DROP TABLE MY_DB.MY_TABLE'))

# build new version of table DBI::dbSendQuery(con,paste('CREATE TABLE PABLE MY_DB.MY_TABLE AS (‘,query_string,’) WITH DATA'))

即使您没有使用 SELECT 子句检索内容,DBI 仍会在每次调用 DBI::dbSendQuery() 后分配一个结果集。 在 DBI::dbSendQuery() 个调用之间使用 DBI::dbClearResult() 尝试一下。

DBI::dbClearResult() 是:

Clear A Result Set
Frees all resources (local and remote) associated with a 
result set. In some cases (e.g., very large result sets) this 
can be a critical step to avoid exhausting resources 
(memory, file descriptors, etc.)

手册页的示例应该给出应该如何调用该函数的提示:

con <- dbConnect(RSQLite::SQLite(), ":memory:")

rs <- dbSendQuery(con, "SELECT 1")
print(dbFetch(rs))

dbClearResult(rs)
dbDisconnect(con)