Hi All Experts in Shiny and Database issues,
I am able to retrieve my database results out and output to a table, without any problems, with the below code, except that after 16 connections, it is a problem.
I know that I have to close the connection to solve this problem. However, if I insert dbDisconnect(con) directly after rs <- dbGetQuery(con, sql), I do not get any results output to the table.
Trying below codes does not work too, meaning there is no output to the table:
rs <- dbSendQuery(con,sql)
data <- dbFetch(rs)
dbHasCompleted(rs)
dbClearResult(rs)
dbDisconnect(con)
Can anyone suggest a working solution, so that I do not have the maximum 16 connection issue? Or did I place the dbDisconnect(con) wrongly?
library(lubridate)
library(RMySQL)
library(DBI)
# server.R
# --------------------------------------------------------
server <- shinyServer(function(input, output, session) {
startime <- reactive({
start_time <-input$starttime
})
endtime <- reactive({
end_time <-input$endtime
})
output$text <- renderText({
paste("You have selected:",input$starttime)
})
query_output <- reactive({
con <- dbConnect(RMySQL::MySQL(), host = "
www.db4free.net", dbname ="punggol",
user = "tanthiamhuat", password = "90795159")
start_time <- startime()
end_time <- endtime()
sql <- paste("select * from PunggolCentral ",
"where DateTime > STR_TO_DATE('",start_time,"', '%Y/%m/%d %H:%i:%s')",
"and DateTime < STR_TO_DATE('",end_time,"', '%Y/%m/%d %H:%i:%s')",sep="")
rs <- dbGetQuery(con, sql)
# rs <- dbSendQuery(con,sql)
# data <- dbFetch(rs)
#dbHasCompleted(rs)
#dbClearResult(rs)
#dbDisconnect(con)
})
output$table <- renderDataTable(query_output())
})