如何从 RShiny 应用程序使用 SQL 命令 UPDATE 和 INSERT INTO?

问题描述 投票:0回答:1

我在 SSMS(SQL Server Management Studio)中创建了一个 SQL Server 表,其中包含 3 列

partNumber
numberOfBoxes
Quantity

我还使用 R 中的 RODBC 库连接到该数据库。众所周知,这是有效的:

dbCon <- odbcConnect("SQL")
df <- sqlFetch(dbCon, "stockExample")

我还尝试创建一个 RShiny 页面,允许用户更新当前库存水平或添加新的库存水平 - 用 SQL 术语来说,

UPDATE
INSERT INTO

我的RShiny页面是这样的:

page2 <- tabPanel(
  title = "Page 2",
 mainPanel(
   titlePanel("Adding Stock"),
   sidebarLayout(
     sidebarPanel(
       helpText("This page allows the user to add stock.")
       ),
     mainPanel(
       "Add/remove stock based on partNumber.",
       
       textInput("ti1", "Part Number:"),
       textInput("ti2", "Number of Boxes:"),
       textInput("ti3", "Quantity:"),
       actionButton("action", "Submit")
     )
   )
 ) 
)

用户输入“

ti1
”、“
ti2
”和“
ti3
”的值。单击
actionButton
将更新 SQL 表,以便当它在另一个页面上或实际上在 SQL 中重新加载时,它将被更新。

我知道我的服务器功能必须从“

action
”获取输入,但我不知道如何从那里继续。

我尝试了几种选择,其中之一是:

output$action <- DBI::dbSendQuery(dbCon, "UPDATE stockExample SET numberOfBoxes = 5 WHERE partNumber = RD001")

此错误为:

Warning: Error in <Anonymous>: unable to find an inherited method for function ‘dbSendQuery’ for signature ‘"RODBC"’
  50: stop
  49: <Anonymous>
  48: DBI::dbSendQuery
  47: server [C:\Users\steve\Desktop\Random\R Projects\'database'/app.R#81]
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘dbSendQuery’ for signature ‘"RODBC"’

我还尝试过其他一些方法,但总而言之,我很难理解它们的语法。是否有 RODBC、DBI 或 DBPLYR 备忘单可以提供我需要的结果?

sql-server shiny rodbc r-dbi
1个回答
0
投票

这是一个基于 sqlite 的示例,展示了如何基于闪亮的输入创建 SQL INSERT 语句:

library(shiny)
library(RSQLite)
library(DBI)

stockExample <- data.frame(partNumber = character(0L), numberOfBoxes = integer(0L), Quantity  = integer(0L), 
                           stringsAsFactors=FALSE)

con <- dbConnect(RSQLite::SQLite(), ":memory:")
if(!"stockExample" %in% dbListTables(con)){
  dbWriteTable(con, "stockExample", stockExample) 
}

ui <- fluidPage(
  textInput("ti1", "Part Number:"),
  numericInput("ti2", "Number of Boxes:", 0L),
  numericInput("ti3", "Quantity:", 0L),
  actionButton("action", "Submit", icon = icon("refresh")),
  tableOutput("tbl")
)

server <- function(input, output, session) {
  stockExample_rv <- reactiveVal(data.frame())
  
  observeEvent(input$action, {
    # also see DBI::dbAppendTable()
    sql <- sqlInterpolate(con,
                          "INSERT INTO stockExample (partNumber, numberOfBoxes, Quantity)
                           VALUES (?partNumber, ?numberOfBoxes , ?Quantity);", 
                          partNumber = input$ti1, numberOfBoxes = input$ti2, Quantity = input$ti3
    )
    DBI::dbExecute(con, sql)
    
    queryString <- sprintf("select partNumber, numberOfBoxes, Quantity from stockExample")
    stockExample_rv(dbGetQuery(con, queryString))
  })
  
  output$tbl <- renderTable({
    stockExample_rv()
  })
}

shinyApp(
  ui = ui,
  server = server,
  onStart = function() {
    onStop(function() {
      dbDisconnect(con)
    })
  }
)

result

另请检查:

https://solutions.posit.co/connections/db/best-practices/run-queries-safely/ https://solutions.posit.co/connections/db/r-packages/pool/

© www.soinside.com 2019 - 2024. All rights reserved.