簡體   English   中英

RODBC無法以閃亮的方式連接-odbcValidChannel(channel)不為TRUE

[英]RODBC can't connect in shiny— odbcValidChannel(channel) is not TRUE

我意識到關於使用光澤連接到RODBC有很多類似的問題。 但是,他們的解決方案都不適合我。

我已經在本地連接到我的SQL Server,沒有問題,但是已發布的閃亮應用程序無法連接。

在本地運行應用程序時,我使用:

myData <- reactive({

##connect to database 

myServer <- "***"
myUser <- "***"
myPassword <- "**"
myDatabase <- "lto"
myDriver <- "ODBC Driver 13 for SQL Server" 

connectionString <- paste0(
  "Driver=", myDriver, 
  ";Server=", myServer, 
  ";Database=", myDatabase, 
  ";Uid=", myUser, 
  ";Pwd=", myPassword)

conn <- odbcDriverConnect(connectionString)

dbhandle <- odbcDriverConnect(connectionString)

query='SELECT [menu_item_id]
            ,[primary_brand_id],[Q1],[Q2],[Q3]
            ,[Q4],[Q5],[Q6],[Q7],[brand],[brand_parent]
            ,[menu_item_name],[course_category],[day_part]
            ,[description],[display_name],[display_with_brand]
            ,[meal_part],[product_category],[reported]
            ,[month],[year],[year_month],[period],[respondent_id]
            ,[generation],[gender],[ethnicity],[income]
            ,[eater_archetype],[survey_type]
            FROM [dbo].[vw_menu_item_responses]
            WHERE [month]=?'

#month<-3

#store results
res <- sqlExecute(channel = dbhandle, 
                  query = query,
                  data = list(input$month),
                  fetch = TRUE,
                  stringsAsFactors = FALSE) 

#close the connection
odbcClose(dbhandle)
#return results
res
})

output$table<- renderDataTable({
d<-myData()
head(d)  
})

嘗試發布應用程序時,我使用:

 myData <- reactive({

##connect to database 
myServer <- "***"
myUser <- "***"
myPassword <- "**"
myDatabase <- "lto"
myDriver <- "FreeTDS;TDS_Version=9.0" 

connectionString <- paste0(
  "Driver=", myDriver, 
  ";Server=", myServer, 
  ";Database=", myDatabase, 
  ";Uid=", myUser, 
  ";Pwd=", myPassword)

conn <- odbcDriverConnect(connectionString)

dbhandle <- odbcDriverConnect(connectionString)


#build query
#query = "SELECT * FROM [my_db].[dbo].[my_table] where [CATEGORY] = '1070'"
#query = "SELECT * FROM [my_db].[dbo].[my_table] where [CATEGORY] = ?"

query='SELECT [menu_item_id]
            ,[primary_brand_id],[Q1],[Q2],[Q3]
            ,[Q4],[Q5],[Q6],[Q7],[brand],[brand_parent]
            ,[menu_item_name],[course_category],[day_part]
            ,[description],[display_name],[display_with_brand]
            ,[meal_part],[product_category],[reported]
            ,[month],[year],[year_month],[period],[respondent_id]
            ,[generation],[gender],[ethnicity],[income]
            ,[eater_archetype],[survey_type]
            FROM [dbo].[vw_menu_item_responses]
            WHERE [month]=?'

#month<-3

#store results
res <- sqlExecute(channel = dbhandle, 
                  query = query,
                  data = list(input$month),
                  fetch = TRUE,
                  stringsAsFactors = FALSE) 

#close the connection
odbcClose(dbhandle)
#return results
res
  })

注意我也嘗試了不同的TDS版本(7.0,8.0)。 另外,問題不在於我的input $ month變量,因為我嘗試將其設置為常量,但仍然無法正常工作。

歡迎任何建議。 請幫忙。

同樣顯然我的用戶名,密碼和服務器實際上不是星號,但是我不想共享的機密信息:)

回答我自己的問題:

我需要在連接字符串中包括端口!!

我變了:

“; Database =”,myDatabase,

至:

“; Port = 1433; Database =”,myDatabase,

我的最終解決方案還使用了TDS_Version 7.0,我不確定這是否是問題的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM