2017-10-18 93 views
1

这似乎是一个旧的重复问题,但没有任何我发现的帖子在我的情况下工作。我有这个简单的SQL查询:R:双引号粘贴,如何摆脱反斜杠

min.t <- "2017-10-17 00:00:00" 
max.t <- "2017-10-17 08:00:00" 
query <- paste0('select * from pred where \"current.time\">\"',min.t,'\" and 
\"current.time\"<\"',max.t,'\"') 
"select * from pred where \"current.time\">\"2017-10-17 00:00:00\" and 
\"current.time\"<\"2017-10-17 08:00:00\"" 

因为你可以看到反斜杠仍然是因为简单的引用。我需要保留查询的简单引号,因为列名包含一个点。如果我从膏去除齿隙,我得到了相同的结果:

paste0('select * from pred where "current.time">"',min.t,'" and 
"current.time"<"',max.t,'"') 
[1] "select * from pred where \"current.time\">\"2017-10-17 00:00:00\" and 
\"current.time\"<\"2017-10-17 08:00:00\"" 

gsub('\\\\', '', query) 

似乎忽略它们。

+0

为什么不使用方括号或反引号(SQLite中逸出符号),而不是双引号(ANSI-SQL中的一般标识符符号)? – Parfait

回答

2

我会词组您的原始SQLite的查询,因为这:

select * 
from pred 
where 
    "current.time" > '2017-10-17 00:00:00' and 
    "current.time" < '2017-10-17 08:00:00'; 

在R,你可以使用精确的上面的查询到一个字符变量,例如

query <- paste0("select * from pred where \"current.time\" > '2017-10-17 00:00:00' ", 
       "and \"current.time\" < '2017-10-17 08:00:00'") 

请注意,我们可以做一个简化,将WHERE条款,并使用BETWEEN,从而导致此:

query <- paste0("select * from pred where \"current.time\" between ", 
       "'2017-10-17 00:00:01' and '2017-10-17 07:59:59'") 
+0

谢谢,但鉴于我的专栏在名称中有一个点,它看起来像我需要一个双引号。尝试你的例子给出: query < - paste0(“select * from pred where current.time>'”,min.t,''“, ”and current.time <'“,max.t,”'“ ) res < - dbGetQuery(tb,query) rsqlite_send_query(conn @ ptr,statement)中的错误: 没有这样的列:current.time –

+0

@XavierPrudent你需要用圆点转义列名是正确的。首先,你真的不应该在列名中使用点。你可以尝试运行我的原始查询,看看它是否有效?然后让我知道是否从R相同的查询作品。 –

+0

它的工作:)我只是困惑理解为什么。你用两个简单的引号替换了。它们与bash有相同的效果吗? –