通过Shining app将信息写入PostgreSQL数据库
需要社区的一些智慧 我的目标是构建一个原始的闪亮应用程序,我将在其中插入一些值。 我对SQL不是非常熟悉,所以我犯了个错误 我有一个远程PostgreSQL数据库,并使用Navicat11 我的测试数据库只有两列-id和message。 我想通过闪亮的应用程序插入id和消息并远程存储 我用了迪安·阿塔利的教程 这是我的密码通过Shining app将信息写入PostgreSQL数据库,r,postgresql,shiny,R,Postgresql,Shiny,需要社区的一些智慧 我的目标是构建一个原始的闪亮应用程序,我将在其中插入一些值。 我对SQL不是非常熟悉,所以我犯了个错误 我有一个远程PostgreSQL数据库,并使用Navicat11 我的测试数据库只有两列-id和message。 我想通过闪亮的应用程序插入id和消息并远程存储 我用了迪安·阿塔利的教程 这是我的密码 # Set libraries library(RPostgreSQL) library(shiny) # Define the fields we want to sav
# 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()
})
}
这是我的错误:
PostgreSqlExecuteStatementConn中的错误,语句,…:
RS-DBI驱动程序:无法检索结果:错误:关系id不存在
第1行:从id中选择*
^
postgresqlQuickSQLconn中的警告,语句,…:
无法创建执行:从id选择**
据我所知,我做了一个错误的sql查询。
有什么想法吗?非常感谢你的帮助 已解决
# 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()
})
}
)
您的INSERT和SELECT尝试插入/查询id列,此时它们应该引用表。表是否被混淆了XX\u XXX?无论哪种方式,SQL都类似于INSERT INTO XX_XXX VALUES…,SELECT将是SELECT*FROM XX_XXX,如果XX_XXX不是真实的表名,则替换真实的表名。根据您提供的链接,表名应该是响应,因此可以尝试该文章中的相同代码,例如INSERT INTO%s VALUES'%s'@bma,非常感谢您这么快的回复。我明白了,所以insert和select应该同时使用id列和消息列。我改为查询错误与PostgreSqlExecuteStatementConn中的侦听错误相同,语句,…:RS-DBI驱动程序:无法检索结果:错误:关系rw_消息不存在第1行:选择*来自数据库名称^postgresqlQuickSQLconn中的警告,语句,…:无法创建执行:从数据库名称中选择*。我在想也许我给数据库起了个错误的名字。但是我只尝试了table和database.table-没有什么我从来没有听说过Shiny,所以我没有太多建议,但是我建议您编写基本的SELECT和INSERT查询,并使用psql Postgres客户端或PGAdmin或任何您手头的GUI来查询Postgres数据库,对您的数据库进行测试。一旦你证明了你的SQL是有效的,那么你就可以在你的应用程序中运行它,更好地理解失败意味着什么。