使用R RODBC参数化SQL查询

使用R RODBC参数化SQL查询,r,parameters,rodbc,R,Parameters,Rodbc,我有一个相当简单的问题 每天,我使用RODBC包在R中执行数据分析。我使用SQL将其连接到数据仓库,并将其移动到R环境中 dbhandle <- odbcDriverConnect('driver={SQL Server};server=SQLSERVER;database=MYDATABASE;trusted_connection=true') degrees <- sqlQuery(dbhandle, "select Inst, ID, DegreeDate, Degree f

我有一个相当简单的问题

每天,我使用RODBC包在R中执行数据分析。我使用SQL将其连接到数据仓库,并将其移动到R环境中

dbhandle <- odbcDriverConnect('driver={SQL Server};server=SQLSERVER;database=MYDATABASE;trusted_connection=true')

degrees <- sqlQuery(dbhandle, "select Inst, ID, DegreeDate, Degree from DEGREE where FY = ('2015') group by Inst, ID, DegreeDate, Degree order by Inst, ID, DegreeDate, Degree", as.is=TRUE)

dbhandle您可以在开始时创建一个输入变量,并将其传递给查询。
例如:

# Change your FY here
input_FY <- 2016

dbhandle <- odbcDriverConnect('driver={SQL Server};server=SQLSERVER;database=MYDATABASE;trusted_connection=true')

degrees <- sqlQuery(dbhandle, paste0("
select Inst, ID, DegreeDate, Degree 
from DEGREE 
where FY = ('", input_FY, "') 
group by Inst, ID, DegreeDate, Degree 
order by Inst, ID, DegreeDate, Degree"), 
as.is=TRUE)
#在此处更改您的FY
input_FY严格来说是MSAccess.exe GUI功能。如果通过ODBC将MS Access作为后端数据库运行(在MS Office软件之外),则带有未知参数的查询将失败,并在进行ODBC调用的脚本上引发错误

因此,您需要使用库(如或)在R中创建一个类似的GUI弹出框以满足此需求,然后将用户的输入值传递到查询中。如果恶意用户运行SQL注入并可能擦除数据或破坏SQL Server数据库,请使用actual(RODBC扩展)执行此操作

下面是一个在会计年度使用组合框的示例(下面的屏幕截图)

library(RDOBC)
library(RODBCext)
library(gWidgets2)
library(gWidgets2tcltk)

options(guiToolkit="tcltk")
GUI功能(预先创建R和SQLServer gif图像)


main窗口如果
RODBC
不支持此功能,那么我猜您可能需要自己实现部分或全部功能。谢谢!但我没有从中得到数据帧我做错什么了吗?它变成了一个字符,
返回什么?它应该返回一个数据帧,我认为它返回了一个字符,可能是因为通道
dbhandle
未打开,或者表
DEGREE
不存在。我真诚的道歉。我修好了,它成功了。我在stackoverflow上使用了更基本的变量名,但忘了更改它们。哇!谢谢你,@MKa!注意:在这个只有答案的字符串连接中没有参数化,如果允许用户输入值,请注意sql注入:!
mainWindow <- function(){

  # TOP OF WINDOW
  win <- gWidgets2::gwindow("Fiscal Year User Input", height = 200, width = 300)

  tbl <- glayout(cont=win, spacing = 8, expand=TRUE)

  # IMAGE
  tbl[1,1] <- gimage(filename = "RSQLServerGUI.gif", 
                     dirname = "/path/to/gif/image", container = tbl)
  # LABEL
  tbl[2,1] <- glabel("Fiscal Year Selection:                      ", container = tbl)
  font(tbl[2,1]) <- list(size=12, family="Arial")

  # COMBO BOX OF FISCAL YEARS
  tbl[3,1, expand=TRUE] <- fiscal_year_cbo <- gcombobox(as.character(c(2012:2018)), 
                                                        selected = 1, editable = TRUE, 
                                                        index=TRUE, container = tbl)
  font(tbl[3,1]) <- list(size=16, family="Arial")

  # COMBO BOX CHANGE FUNCTION (2012 - 2018)
  addHandlerChanged(fiscal_year_cbo, handler=function(...)  {
    fiscal_year_value <- svalue(fiscal_year_cbo)           # GET USER SELECTED VALUE
    gmessage(paste("You selected FY:", fiscal_year_value))

    degrees <- getDegreesData(fiscal_year_value)           # GET DATABASE DATA 
    dispose(win)                                           # CLOSE WINDOW
  })

}
getDegreesData <- function(fy_param) {

    dbhandle <- odbcDriverConnect('driver={SQL Server};server=SQLSERVER;database=MYDATABASE;trusted_connection=true')

    # PREPARED STATEMENT (NO CONCATENATED DATA)
    strSQL <- "select Inst, ID, DegreeDate, Degree 
               from DEGREE 
               where FY = ?
               group by Inst, ID, DegreeDate, Degree 
               order by Inst, ID, DegreeDate, Degree"

    # PASS PARAMETER TO RETURN DATAFRAME
    sql_df <- sqlExecute(dbhandle, strSQL, fy_param, fetch=TRUE)
    odbcClose(dbHandle)

    return(sql_df)
}
m <- mainWindow()