R 是否有一个';清洁剂&x27;连接查询字符串的方法?
我正在将查询字符串传递给连接,结果如下所示:R 是否有一个';清洁剂&x27;连接查询字符串的方法?,r,dbi,rodbc,dbplyr,R,Dbi,Rodbc,Dbplyr,我正在将查询字符串传递给连接,结果如下所示: select game_name, month, count(*) as count from device_metrics.mtu_events where YEAR = '2019' and month between '07' and '09' group by game_name, month order by game_name asc, month asc 如果我将上述块作为单个字符串传递到DBI::dbGetQuery(co
select game_name, month, count(*) as count
from device_metrics.mtu_events
where YEAR = '2019' and month between '07' and '09'
group by game_name, month
order by game_name asc, month asc
如果我将上述块作为单个字符串传递到DBI::dbGetQuery(con,myquery)
但是日期是闪亮应用程序中的一个变量,因此我尝试创建一个函数来生成查询字符串:
my_query <- function(start_date, end_date) {
yr <- year(ymd(start_date))
month_start <- month(ymd(start_date))
month_end <- month(ymd(end_date))
query <- paste0(
"select game_name, month, count(*) as count
from device_metrics.mtu_events
where YEAR = ", yr, " and month between ", month_start, " and ", month_end,
" group by game_name, month
order by game_name asc, month asc")
return(query)
}
my_query这里有两个选项,我们可以在其中引用(”
)作为字符串的输入,如月
,年
函数返回数值
my_query <- function(start_date, end_date) {
yr <- year(ymd(start_date))
month_start <- month(ymd(start_date))
month_end <- month(ymd(end_date))
query <- paste0(
"select game_name, month, count(*) as count
from device_metrics.mtu_events
where YEAR = '", yr, "' and month between '", month_start, "' and '", month_end,
"' group by game_name, month
order by game_name asc, month asc")
return(query)
}
这里有两个选项,我们可以(”
)将输入作为字符串引用为月
,年
函数返回数值
my_query <- function(start_date, end_date) {
yr <- year(ymd(start_date))
month_start <- month(ymd(start_date))
month_end <- month(ymd(end_date))
query <- paste0(
"select game_name, month, count(*) as count
from device_metrics.mtu_events
where YEAR = '", yr, "' and month between '", month_start, "' and '", month_end,
"' group by game_name, month
order by game_name asc, month asc")
return(query)
}
我们可以使用gsubfn中的fn$
执行字符串插值。为了重现性,我们将使用NULL代替实际连接,并使用fn$list
代替fn$dbGetQuery
,但您可以同时替换这两种连接。进行此操作时,请勿在名称中使用下划线或点
library(gsubfn)
yr <- 2019
monthStart <- '07'
monthEnd <- '09'
con <- NULL
fn$list(con, "select game_name, month, count(*) as count
from device_metrics.mtu_events
where YEAR = '$yr' and month between '$monthStart' and '$monthEnd'
group by game_name, month
order by game_name asc, month asc")
库(gsubfn)
yr我们可以使用gsubfn中的fn$
执行字符串插值。为了重现性,我们将使用NULL代替实际连接,并使用fn$list
代替fn$dbGetQuery
,但您可以同时替换这两种连接。进行此操作时,请勿在名称中使用下划线或点
library(gsubfn)
yr <- 2019
monthStart <- '07'
monthEnd <- '09'
con <- NULL
fn$list(con, "select game_name, month, count(*) as count
from device_metrics.mtu_events
where YEAR = '$yr' and month between '$monthStart' and '$monthEnd'
group by game_name, month
order by game_name asc, month asc")
库(gsubfn)
yr我想你需要where YEAR='”,yr,“'and month between'”,month_start,“'and'”,month_end,“'group_by grame_name,month
,因为'yr',month_start',month_end'周围的单引号不在paste0
中,这是否与下面的month_start%str相等
我猜month(Sys.Date())
返回数值Trymy\u查询坦率地说,我更喜欢使用DBI::dbBind
进行参数化查询,而不是进行引用之类的查询()。(我不熟悉雅典娜的方言,不确定它的支持程度。)一些关于参数化SQL查询的想法,我想你需要where YEAR='、yr、'and month between'、month_start、'and'、month_end、'group_by grame_name、month
,因为单引号围绕着'yr',month_start',“month_end”不在paste0
中,这是否等同于,例如,进一步的month_start%str()
我猜month(Sys.Date())
返回数值Trymy_查询坦率地说,我更喜欢使用DBI::dbBind
进行参数化查询,而不是进行引用之类的查询()。(我不熟悉雅典娜的方言,不确定它的支持程度。)一些关于参数化SQL查询的想法,我不会依赖手动勾选,sQuote
将更健壮地处理格式错误的参数(和SQL注入)。@r2evans是的,但可能必须将花哨的引号指定为FALSEsQuote(1)[1]“'1'
当然,但是options(useFancyQuotes=FALSE)
修复了这一问题。我不会依赖手动勾选,sQuote
会更健壮地处理格式错误的参数(和sql注入)。@r2evans是的,但可能必须将这个奇特的引号指定为FALSEsQuote(1)[1]“'1'
当然,但是options(useFancyQuotes=FALSE)
修复了这一问题。