【问题标题】:R function dbGetQuery returns 1x1 table with name from function dbConnectR 函数 dbGetQuery 从函数 dbConnect 返回名称为 1x1 的表
【发布时间】:2020-06-18 09:55:36
【问题描述】:

我在 R 中有以下问题。

我使用 RPostgres 包函数 dbConnect 连接到数据库:

dbConnect(m, dbname=dbname, host=host, port=port, user=example, password=password)

用户名是'example' 数据库中一张表的名字也是'example'。

运行时:dbGetQuery(db, "SELECT * from example") 而不是获取表, 我得到只有一个值的 1x1 数据框:示例(与 dbConnect 函数中的用户值相同)

似乎命令 dbGetQuery(db, "SELECT * from example")dbConnect(m, dbname=dbname, host=host, port=port, user=example, password=password) 而不是从数据库返回表。

你有什么想法可以解决这个问题吗?

【问题讨论】:

  • 您是否将连接对象传递给 get 命令?所以,conn <- dbConnect(m, dbname=dbname, host=host, port=port, user=example, password=password),然后是dbGetQuery(conn, "SELECT * from example")
  • 是的,我做到了,所以这不是原因:(
  • 与模式有关吗?如果你这样做select * from schema.example会发生什么?
  • 这个问题只发生在名为example 的表上还是所有表都发生?
  • 只有这个,你写的这个解决方案select * from schema.example有效,谢谢:)

标签: r


【解决方案1】:

添加我之前的评论作为答案: 看起来dbGetQuery() 在用户名和表名之间感到困惑。将架构名称添加到查询中。

conn <- dbConnect(m, dbname=dbname, host=host, port=port, user=example, password=password)
dbGetQuery(conn, "SELECT * from schema.example")

【讨论】:

    猜你喜欢
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多