使用rpostgres,我应该用什么来设置角色...”对于桌子,我会写信给DB?

发布于 2025-01-26 10:51:42 字数 1022 浏览 3 评论 0 原文

我是通过R连接到数据库的新手,我正在尝试找到最大程度地减少错误和问题的最佳实践。我将表从R上传到Postgres数据库,我需要将权限设置为我知道的一个名称的某个组。

我正在尝试找出各种DBI功能的不同行为和最佳实践,以免我不小心犯错而弄乱数据库。

我不知道我是否应该使用dbexecute()或dbsendquery()。我已经阅读了两个功能的R文档,并了解他们执行SQL命令以修改连接的数据库。我知道dbexecute()告诉我受影响的行的数量,但是dbsendquery()似乎也是如此。 dbexecute()似乎使用dbsendstatement(),但这并不能帮助我理解差异,因为它看起来相似。

我无法解释以下两个示例中看到的行为。他们俩都在做同样的事情吗?他们俩都在工作吗?一种方法比另一种更好或更安全吗?

示例1

res <- dbExecute(con,'set role certain_group')
print(res)          # output is: [1] 0
dbClearResult(res)  # output is: Error in (function (classes, fdef, mtable)  : 
#  unable to find an inherited method for function ‘dbClearResult’ for signature ‘"integer"’

示例2

res2 <- dbSendQuery(con,'set role certain_group')
print(res2)         # output is: <PqResult>
  SQL  set role certain_group
  ROWS Fetched: 0 [complete]
       Changed: 0
dbClearResult(res)  # no output in console

最终注意:我更喜欢使用rpostgres软件包,而不是其他选项。

I'm new to connecting to databases via R, and I am trying to find best practices to minimize errors and problems. I am uploading a table from R to a postgres database, and I need to set the permissions to a certain group that I know the name of.

I'm trying to figure out the different behaviors and best practices for various DBI functions, so that I don't accidentally make a mistake and mess up the database.

I don't know whether I should use dbExecute() or dbSendQuery(). I've read the R documentation for both functions, and understand that they execute sql commands to modify the connected database. I understand that dbExecute() tells me the number of rows affected, but dbSendQuery() seems to also. dbExecute() seems to use dbSendStatement(), but this does not help me understand the difference because it seems similar.

I can't explain the behavior I see in these two examples below. Are they both doing the same thing? Are they both working? Is one way better or safer than the other?

Example 1

res <- dbExecute(con,'set role certain_group')
print(res)          # output is: [1] 0
dbClearResult(res)  # output is: Error in (function (classes, fdef, mtable)  : 
#  unable to find an inherited method for function ‘dbClearResult’ for signature ‘"integer"’

Example 2

res2 <- dbSendQuery(con,'set role certain_group')
print(res2)         # output is: <PqResult>
  SQL  set role certain_group
  ROWS Fetched: 0 [complete]
       Changed: 0
dbClearResult(res)  # no output in console

Final note: I prefer to use the RPostgres package as opposed to other options.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2025-02-02 10:51:42

在SQL中,大多数命令属于两种类型:影响数据的操作查询(即, insert update , delete , drop /code>)或返回数据的结果集(即, select )。

在R的DBI中,不同的方法每个文档触发了这两种类型的命令:

  • dbexecute 主要用于操作查询,实际上是 dbsendStatement + dbgetRowsed + + + + + + + + dbclearresult 。每个文档:

dbexecute:执行更新语句,受影响的行查询编号,然后关闭结果集

  • dbgetquery 主要用于迁移到数据帧的结果集查询,实际上是 dbsendquery + +的包装器 dbfetch + dbclearresult 。每个文档:

dbgetquery:发送查询,检索结果,然后清除结果集

以清晰的结果设置为“ dbexecute” and dbsendquery 应运行任何类型的SQL语句,但其返回值不同。取决于包装味(即, odbc roracle rmysql rpostgresql ),您可能需要使用 dbsendquery 运行操作语句,尤其是使用 dbbind 绑定参数。但是 dbexecute 永远不会返回数据框架!

您的Postgres特定 SET 语句是一个特殊的操作查询。因此,只需调用 dbexecute 即可运行并检索受影响的任何行。另外,调用 dbsendquery + dbgetRowsed + dbclearresult 可能达到与 dbexececute 的相同结果。

In SQL, most commands fall under two types: action queries that affect data (i.e., INSERT, UPDATE, DELETE, DROP) or resultset queries that return data (i.e., SELECT).

In R's DBI, different methods trigger these two types of commands per documentation:

  • dbExecute is mainly used for action queries and is actually a wrapper for dbSendStatement + dbGetRowsAffected + dbClearResult. Per docs:

dbExecute: Execute an update statement, query number of rows affected, and then close result set

  • dbGetQuery is mainly used for resultset queries migrating results to a data frame and is actually a wrapper for dbSendQuery + dbFetch + dbClearResult. Per docs:

dbGetQuery: Send query, retrieve results and then clear result set

With that said, both dbExecute and dbSendQuery should run any type of SQL statement but their return values differ. Depending on the package flavor (i.e., odbc, ROracle, RMySQL, RPostgreSQL), you may need to use dbSendQuery to run action statements particularly for binding parameters with dbBind. But dbExecute will never return a data frame!

Your Postgres-specific SET statement is a special action query. Therefore, simply call dbExecute to run and retrieve any rows affected. Alternatively, call dbSendQuery + dbGetRowsAffected + dbClearResult to possibly achieve the same result as dbExecute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文