尝试在 SQL 服务器上设置参数化查询时出现 dbBind 函数错误
dbBind function error when trying to set up parameterized query on SQL Server
我正在尝试使用 DBI
程序包 dbSendQuery
、dbBind
和 dbFetch
工作流程进行一些参数化查询。
我可以使用 dbSendQuery
使用 ?
作为变量的占位符。但是,当我 运行 dbBind
时,出现以下错误:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'dbBind' for signature '"Microsoft SQL Server"'
我的全部代码如下所示:
library(odbc)
library(DBI)
test_connection <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "MyServer",
Database = "MyDataBase")
test_query <- dbSendQuery(test_connection,
"SELECT TOP 2 col1, col2 FROM MyTable WHERE col2 = ?")
dbBind(test_connection,
list('value'))
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'dbBind' for signature '"Microsoft SQL Server"'
任何人都可以向我解释这个错误的原因吗?
如果我在 dbSendQuery
语句中硬编码 value
并使用 dbFetch
查询就可以工作。
在 dbBind
中,您应该使用查询对象而不是连接对象。在您的示例中:
dbBind(test_query, list('value'))
我正在尝试使用 DBI
程序包 dbSendQuery
、dbBind
和 dbFetch
工作流程进行一些参数化查询。
我可以使用 dbSendQuery
使用 ?
作为变量的占位符。但是,当我 运行 dbBind
时,出现以下错误:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'dbBind' for signature '"Microsoft SQL Server"'
我的全部代码如下所示:
library(odbc)
library(DBI)
test_connection <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "MyServer",
Database = "MyDataBase")
test_query <- dbSendQuery(test_connection,
"SELECT TOP 2 col1, col2 FROM MyTable WHERE col2 = ?")
dbBind(test_connection,
list('value'))
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'dbBind' for signature '"Microsoft SQL Server"'
任何人都可以向我解释这个错误的原因吗?
如果我在 dbSendQuery
语句中硬编码 value
并使用 dbFetch
查询就可以工作。
在 dbBind
中,您应该使用查询对象而不是连接对象。在您的示例中:
dbBind(test_query, list('value'))