在闪亮的应用程序中从数据库中删除选定的表行

时间:2015-11-22 00:36:54

标签: r shiny rmysql

我想在shinyapp中单击“删除”按钮时从MySql数据库中删除所选行。 ui.r和server.r的代码如下: 它连接到MySQl,我可以将数据保存到mySQL,但我无法删除和更新表。 1:连接数据库

deleteData <- function() {
    # Connect to the database
     db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host, 
                         port = options()$mysql$port, user = options()$mysql$user, 
                         password = options()$mysql$password)
      # Construct the deleting query
      query <- sprintf("DELETE selctedrow FROM %s", table)
     # Submit the fetch query and disconnect
      data <- dbGetQuery(db, query)
     dbDisconnect(db)
      data
  }

删除数据的功能:

observeEvent(input$delete.button, {
    deleteData (formData())
  })

单击“删除”按钮时,删除所选的数据行

ui <- fluidPage(
      #use shiny js to disable the ID field
      shinyjs::useShinyjs(),
      #DT::dataTableOutput("responses"),
      shinyjs::disabled(textInput("id", "Id", "0")),

      textInput("name", "Name", ""),
      checkboxInput("used_shiny", "I've built a Shiny app in R before", FALSE),
      sliderInput("r_num_years", "Number of years using R", 0, 25, 2, ticks = FALSE),

      actionButton("submit", "Submit",icon = icon("plus-circle"),class = "btn-primary"),
      # Delete button 
      actionButton(inputId = "delete", label = "Delete", icon = icon("minus-circle"),class = "btn-primary"),
      #NEW button
      actionButton("new", "Reset",icon = icon("refresh"),class = "btn-primary")
    ))
    ,box(
      title = "KPIs", status = "primary", solidHeader = TRUE,
      collapsible = TRUE,
     DT::dataTableOutput("responses"), tags$hr()

    )),

ui.r的代码

Listening on http://127.0.0.1:5894
Warning: Unhandled error in observer: unused argument (formData())
observeEvent(input$delete.button)

我得到的错误讯息:

List<string> emails = new List<string>();
emails.Add("john.doe@domain1.com");
emails.Add("jane_doe@domain1.com");
emails.Add("patricksmith@domain2.com");
emails.Add("erick.brown@domain3.com");

var q = emails.GroupBy(m => m.Split('@')[1]).Select(g => new List<string>(g)).Interleave();

1 个答案:

答案 0 :(得分:0)

只需将当前表单的id参数传递给该函数即可。然后将id连接到DELETE查询的WHERE子句中。顺便说一下,DELETE语句不会占用任何字段,因为它会删除整个行而不是一个特定的列。

用户界面

ui <- fluidPage(
  #use shiny js to disable the ID field
  shinyjs::useShinyjs(),
  #DT::dataTableOutput("responses"),
  shinyjs::disabled(textInput("id", "Id", "0")),

  selectInput("ID", 
              label = "Choose ID from list",
              choices = c(1:50), 
              selected = "1"),   

  textOutput("successtext"),

  textInput("name", "Name", ""),
  checkboxInput("used_shiny", "I've built a Shiny app in R before", FALSE),
  sliderInput("r_num_years", "Number of years using R", 0, 25, 2, ticks = FALSE),

  actionButton("submit", "Submit",icon = icon("plus-circle"),class = "btn-primary"),
  # Delete button 
  actionButton(inputId = "delete", label = "Delete", icon = icon("minus-circle"),class = "btn-primary"),
  #NEW button
  actionButton("new", "Reset",icon = icon("refresh"),class = "btn-primary")
))
,box(
  title = "KPIs", status = "primary", solidHeader = TRUE,
  collapsible = TRUE,
 DT::dataTableOutput("responses"), tags$hr()

)),

服务器端

observeEvent(input$delete.button, {
     deleteData(input$ID)         
})

deleteData <- function(formid) {
     # Connect to the database
     db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host, 
                         port = options()$mysql$port, user = options()$mysql$user, 
                         password = options()$mysql$password)

     # Construct the deleting query
     query <- sprintf("DELETE FROM %s WHERE id = %d", table, formid)

     # Submit the fetch query and disconnect
     rs <- try(dbSendQuery(db, query))
     if(inherits(rs, ErrorClass)){ 
               output$successtext <- renderText({paste0("Failed to remove ID ", 
                          input$ID, " from database table. Please contact administrator.")})
               } 
     } else {
               output$successtext <- renderText({paste0("Successfully removed ID ", 
                          input$ID, " from database table!")})
               } 
     }
     dbDisconnect(db)
}

此外,请务必清理并验证表单数据,尤其是id字段。 DELETE是一个非常强大的功能,当应用程序收到用户输入的数据时,sql injection始终是一个问题。