RJDBC:R 到 Oracle 不能删除或删除 TABLE
RJDBC: R to Oracle cannot DELETE or DROP TABLE
我正在使用 RJDBC 连接到本地数据库。这使我可以使用 dbGetQuery
轻松地进行 SELECT 查询,并使用 dbWriteTable
创建 TABLE。
但是,我无法找到直接从我的 R 控制台删除 TABLE 或 DELETE 或 SELECT INTO 的方法。当我直接在 SQL Developer 中执行这些操作时,这些操作有效,但当我将查询从 R 传递到数据库时则无效。
如何使用 R 执行不是 SELECT 语句的数据库记录操作?
我会尝试使用不同的类型。
dbGetQuery
基于查找和迭代数据库而不是操纵它的记录。
问了类似的问题 before;
我找不到很好的 R 示例,但如果有帮助,可以找到一个很好的 java 示例 here:
编辑:
我找到我说的类型了!无论如何,我花了一段时间 - sqlQuery
允许你 运行 几乎任何查询,即 - 数据库记录中的更改。我从 this 来源修改的示例:
res <- sqlQuery(con1,"DELETE TABLE TESTDATA", errors=FALSE)
# res will now hold the result of the query.
# -1 means error, otherwise iteration is sucessful, and it will hold the number of rows affected.
if (res == -1){ #if something messed up
cat ("An error has occurred.\n")
msg <- odbcGetErrMsg(con1) #Use your connection for this.
print (msg)
} else {
cat ("Table was deleted successfully.\n")
}
编辑 2:
我把它与 RODBC 混淆了,但是没有理由担心,因为我也找到了 RJDBC 的替代品!它被称为,dbSendUpdate
。示例:
# Assuming you have the connection saved as conn; these example shows how to use dbSendUpdate to create tables and insert values.
# You could use it with every non-selective query, that is, which manipulates the record (update,delete,insert,drop etc.)
# create table, with dbSendUpdate:
dbSendUpdate(conn, "CREATE TABLE foo(a INT,b VARCHAR(100))")
# insert value, bind parameters to placeholders in statement:
dbSendUpdate(conn, "INSERT INTO foo VALUES(?,?)", 42, "bar")
# feel free to modify the query itself, these are just example values.
这类似于另一个已回答的问题here
顾名思义,dbGetQuery() 基本上用于发送查询和接收结果。
如果你想像 'drop table' 等一样向数据库发送一般声明。
你可以使用:
dbSendUpdate(connection_object, "drop table table_name")
我正在使用 RJDBC 连接到本地数据库。这使我可以使用 dbGetQuery
轻松地进行 SELECT 查询,并使用 dbWriteTable
创建 TABLE。
但是,我无法找到直接从我的 R 控制台删除 TABLE 或 DELETE 或 SELECT INTO 的方法。当我直接在 SQL Developer 中执行这些操作时,这些操作有效,但当我将查询从 R 传递到数据库时则无效。
如何使用 R 执行不是 SELECT 语句的数据库记录操作?
我会尝试使用不同的类型。
dbGetQuery
基于查找和迭代数据库而不是操纵它的记录。
问了类似的问题 before;
我找不到很好的 R 示例,但如果有帮助,可以找到一个很好的 java 示例 here:
编辑:
我找到我说的类型了!无论如何,我花了一段时间 - sqlQuery
允许你 运行 几乎任何查询,即 - 数据库记录中的更改。我从 this 来源修改的示例:
res <- sqlQuery(con1,"DELETE TABLE TESTDATA", errors=FALSE)
# res will now hold the result of the query.
# -1 means error, otherwise iteration is sucessful, and it will hold the number of rows affected.
if (res == -1){ #if something messed up
cat ("An error has occurred.\n")
msg <- odbcGetErrMsg(con1) #Use your connection for this.
print (msg)
} else {
cat ("Table was deleted successfully.\n")
}
编辑 2:
我把它与 RODBC 混淆了,但是没有理由担心,因为我也找到了 RJDBC 的替代品!它被称为,dbSendUpdate
。示例:
# Assuming you have the connection saved as conn; these example shows how to use dbSendUpdate to create tables and insert values.
# You could use it with every non-selective query, that is, which manipulates the record (update,delete,insert,drop etc.)
# create table, with dbSendUpdate:
dbSendUpdate(conn, "CREATE TABLE foo(a INT,b VARCHAR(100))")
# insert value, bind parameters to placeholders in statement:
dbSendUpdate(conn, "INSERT INTO foo VALUES(?,?)", 42, "bar")
# feel free to modify the query itself, these are just example values.
这类似于另一个已回答的问题here
顾名思义,dbGetQuery() 基本上用于发送查询和接收结果。
如果你想像 'drop table' 等一样向数据库发送一般声明。 你可以使用:
dbSendUpdate(connection_object, "drop table table_name")