【问题标题】:Writing information into PostgreSQL database through Shiny app通过 Shiny 应用程序将信息写入 PostgreSQL 数据库
【发布时间】:2018-06-23 14:38:14
【问题描述】:

需要社区的一些智慧。

我的目标是构建一个原始的 Shiny 应用程序,我将在其中插入一些值。 我对 SQL 不是很熟悉,所以迷路了。

我有一个远程 PostgreSQL 数据库并使用 Navicat 11。

我的测试数据库只有两列 - “id”和“message”。 我想通过闪亮的应用程序插入id和消息并远程存储。

我使用了 Dean Attali 的教程 Persistent data storage in Shiny apps

这是我的代码

# Set libraries
library(RPostgreSQL)
library(shiny)

# Define the fields we want to save from the form
fields <- c("id", "message")

# Shiny app with two fields that the user can submit data for
shinyApp(
ui = fluidPage(
DT::dataTableOutput("responses", width = 300), tags$hr(),
textInput("id", "ID", ""),
textInput("message", "MESSAGE", ""),
actionButton("submit", "Submit")
),

server = function(input, output, session) {

databaseName <- "XXXXX"
table <- "XX_XXX"
psql <- dbDriver("PostgreSQL")

saveData <- function(data) {
  # Connect to the database
  pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXXXX", port = XXXX, user = "XXXX", password = "XXXXX")
  # Construct the update query by looping over the data fields
  query <- sprintf(
    "INSERT INTO id (id) VALUES ('message')",
    table, 
    paste(names(data), collapse = ", "),
    paste(data, collapse = "', '")
  )
  # Submit the update query and disconnect
  dbGetQuery(pcon, query)
  dbDisconnect(pcon)
}

  loadData <- function() {
  # Connect to the database
  pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXXXX", port = XXXX, user = "XXXX", password = "XXXXX")
  # Construct the fetching query
  query <- sprintf("SELECT * FROM id", table)
  # Submit the fetch query and disconnect
  data <- dbGetQuery(pcon, query)
  dbDisconnect(pcon)
  data
}


# Whenever a field is filled, aggregate all form data
formData <- reactive({
  data <- sapply(fields, function(x) input[[x]])
  data
})

# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
  saveData(formData())
})

# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
  input$submit
  loadData()
})     

} )

这是我的错误:

postgresqlExecStatement(conn, statement, ...) 中的错误:

RS-DBI 驱动程序:(无法检索结果:错误:关系“id”不存在

第 1 行:SELECT * FROM id ^ )

postgresqlQuickSQL(conn, statement, ...) 中的警告:

无法创建执行:SELECT * FROM id*

我理解,我做了一个错误的 sql 查询。 有任何想法吗?非常感谢您的帮助!

【问题讨论】:

  • 您的 INSERT 和 SELECT 正在尝试插入/查询 id 列,此时它们应该引用表。表格是否被混淆(XX_XXX)?无论哪种方式,SQL 都类似于INSERT INTO "XX_XXX" VALUES (...),而 SELECT 则为SELECT * FROM "XX_XXX"(如果 XX_XXX 不是真实名称,则替换真实表名)。
  • 根据您提供的链接,表名应该是responses,所以可以尝试该文章中的相同代码,例如。 "INSERT INTO %s (%s) VALUES ('%s')"
  • @bma,非常感谢您的快速回复。我明白了,因此 insert 和 select 应该适用于两个列(“id”和“message”)。我更改为查询
  • 错误是一样的 听127.0.0.1:5455 postgresqlExecStatement(conn, statement, ...) 中的错误:RS-DBI 驱动程序:(无法检索结果:错误:关系“rw_messages”确实不存在 LINE 1: SELECT * FROM (database name) ^ ) postgresqlQuickSQL(conn, statement, ...) 中的警告:无法创建执行:SELECT * FROM (database name)。我在想也许我给数据库起了错误的名字。但我只尝试了 table 和 database.table - 没有
  • 我从未听说过 Shiny,所以我没有太多建议可以提供,但我建议您编写基本的 SELECT(和 INSERT)查询,并使用 @987654330 针对您的数据库进行测试@ Postgres 客户端,或 PGAdmin,或您手头的任何用于查询 Postgres 数据库的 GUI。一旦你证明你的 SQL 工作正常,你就可以在你的应用程序中运行它,更好地了解失败的含义。

标签: r postgresql shiny


【解决方案1】:

已解决。

# Set libraries
library(RPostgreSQL)
library(shiny)

# Define the fields we want to save from the form
fields <- c("id", "message")

# Shiny app with two fields that the user can submit data for
shinyApp(
ui = fluidPage(
DT::dataTableOutput("responses", width = 300), tags$hr(),
textInput("id", "ID", ""),
textInput("message", "MESSAGE", ""),
actionButton("submit", "Submit")
),
server = function(input, output, session) {


psql <- dbDriver("PostgreSQL")

saveData <- function(data) {
  # Connect to the database
  pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXX", port = XXXX, user 
= "UserX", password = "PaswordX")
  # Construct the update query by looping over the data fields
  query <- paste0("INSERT INTO table_name.schema_name (message) VALUES ( $1 
)") 
  # Submit the update query and disconnect
  dbSendQuery(pcon, query, params=data[["message"]]) 
  dbDisconnect(pcon)
}

loadData <- function() {
  # Connect to the database
  pcon <- dbConnect(psql, dbname = "XXX", host = "XXXXX", port = XXXX, user = "UserX", password = "PaswordX")
  # Construct the fetching query
  query <- sprintf("SELECT * FROM table_name.schema_name") 
  # Submit the fetch query and disconnect
  data <- dbGetQuery(pcon, query)
  dbDisconnect(pcon)
  data
}



# Whenever a field is filled, aggregate all form data
formData <- reactive({
  data <- sapply(fields, function(x) input[[x]])
  data
})

# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
  saveData(formData())
})

# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
  input$submit
  loadData()
})     
}
)

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 2019-07-02
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多