Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 是否有一个';清洁剂&x27;连接查询字符串的方法?_R_Dbi_Rodbc_Dbplyr - Fatal编程技术网

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())
返回数值Try
my\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())
返回数值Try
my_查询坦率地说,我更喜欢使用
DBI::dbBind
进行参数化查询,而不是进行引用之类的查询()。(我不熟悉雅典娜的方言,不确定它的支持程度。)一些关于参数化SQL查询的想法,我不会依赖手动勾选,
sQuote
将更健壮地处理格式错误的参数(和SQL注入)。@r2evans是的,但可能必须将花哨的引号指定为FALSE
sQuote(1)[1]“'1'
当然,但是
options(useFancyQuotes=FALSE)
修复了这一问题。我不会依赖手动勾选,
sQuote
会更健壮地处理格式错误的参数(和sql注入)。@r2evans是的,但可能必须将这个奇特的引号指定为FALSE
sQuote(1)[1]“'1'
当然,但是
options(useFancyQuotes=FALSE)
修复了这一问题。