Shiny apps don't work successfully when inserting values into a mysql table

Viewed 27

Why does the following code cause this error message:

Error in rbind.data.frame(..., stringsAsFactors = FALSE) : numbers of columns of arguments do not match

The table in the database (DB) looks correct, but the shiny app does not publish successfully.

The logs file says:

Error in value[3L] : Failed to connect to database: Error: Can't connect to MySQL server on '127.0.0.1' (111)

library(DT)
library(shiny)
library(DBI)
library(RMySQL)

con <- dbConnect(MySQL(), 
                 host = "127.0.0.1",
                 user = "root", 
                 password = "123456789",
                 dbname = "DB",
                 port = 3306
)

##################################################

ui <- fluidPage(
  
  titlePanel("Employee Complaints"),
  
  sidebarLayout(
    sidebarPanel(
      width = 3,
      textInput("name", "Your Name:"),
      textAreaInput("suggestion", "Your suggestion goes here:",
                    width = "100%",
                    height = 250),

      actionButton("submit", "Submit")
    ),
    
    mainPanel(
      tabsetPanel(
        tabPanel("Post", DTOutput("textOut"))
      )
    )
  )
)

server <- function(input, output, session) {
  
  observeEvent(input$submit, {
    
    dbExecute(
      conn = con,
      statement = paste0('INSERT INTO employee (name, suggestion) VALUES (','"', input$name, '","', input$suggestion, '")')
    )
    
    session$reload()

  })
  
  D = reactive({
    dbGetQuery(con, "SELECT * FROM employee")
  })

  output$textOut <- renderDT({
    D()
  })

}

shinyApp(ui = ui, server = server)
0 Answers
Related