1

我的目标是在数据库中创建一个表,然后用数据填充它。这是我的代码:

library(ROracle)

# ... "con" is the connection string, created in an earlier stage!

# 1 create example
testdata <- data.frame(A = c(1,2,3), B = c(4,5,6))
# 2 create-statement
createTable <- paste0("CREATE TABLE TestTable(", paste(paste(colnames(testdata), c("integer", "integer")), collapse = ","), ")")
# 3 send and execute query
dbGetQuery(con, createTable)
# 4 write example data
dbWriteTable(con, "TestTable", testdata, row.names = TRUE, append = TRUE)

我已经成功了几次。该表已创建并填充。

现在步骤 4 不再起作用,R 在执行之后返回 TRUE dbWriteTable。但桌子仍然是空的。

我知道这是一个模糊的问题,但有人知道这里可能出了什么问题吗?

4

1 回答 1

0

我找到了解决我的问题的方法。在步骤 3 中创建表后,您必须提交!之后,数据被写入表中。

library(ROracle)

# ... "con" is the connection string, created in an earlier stage!
# 1 create example
testdata <- data.frame(A = c(1,2,3), B = c(4,5,6))
# 2 create-statement
createTable <- paste0("CREATE TABLE TestTable(", paste(paste(colnames(testdata), c("integer", "integer")), collapse = ","), ")")
# 3 send and execute query
dbGetQuery(con, createTable)
# NEW LINE: COMMIT!
dbCommit(con)
# 4 write example data
dbWriteTable(con, "TestTable", testdata, row.names = TRUE, append = TRUE)
于 2017-03-31T11:26:45.437 回答