Getting this error "first argument is not an open RODBC channel"

Viewed 5050

I have connected R to SQL through ODBC. I'm done with fetching the database,also sort it ascending & assigned to "Resolution1" but now when i tried saving it, an error appeared:

first argument is not an open RODBC channel

My code is shown below:

library(RODBC)
channel<-odbcConnect("RSD")
sqlt<-sqlFetch(channel, "Resolution")
view(sqlt)
Resolution1<-(sqlQuery(channel, "select * from Resolution order by WLCODE,YEAR_OBS ASC"))
View(Resolution1)
sqlSave(Resolution1)
1 Answers

I have often the same issue (with ODBC).

I've created this function

connexion_bdd_txt <- paste0('driver={ODBC Driver 11 for SQL Server};bla bla;')
react <- reactiveValues(connexion_bdd = NULL)

shinyServer(function(input, output,session) { 

  connexion_courante  <- function () {

      tryCatch(
      {
          odbcGetInfo(react$connexion_bdd)
      },
        error=function(e) {    
          print("connection lost, reconnection (E)")
          react$connexion_bdd<-odbcDriverConnect(connexion_bdd_txt )
        },
        warning=function(w) {  print("connection lost (W)")
        }
      )

      return(react$connexion_bdd)

  }
  ...
  Resolution1<-(sqlQuery(connexion_courante(), "select * from Resolution order by WLCODE,YEAR_OBS ASC"))

But I've not found how to prevent deconnexion.

Related