2017-10-18 88 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有相同的效果嗎? –