问题
From This post and This post, I got a way to write an rsqlite dynamic command. However, it doesn't work for me. My data looks like:
Id <- c(34, 22, 86)
sqlcmd <- paste("select col1, col2 from DB where ItemId =", Id, sep="")
Df <- dbGetQuery(conn, sqlcmd)
My sqlcmd
gives me a list of strings as
"select col1, col2 from DB where STOREID =34"
"select col1, col2 from DB where STOREID =22"
"select col1, col2 from DB where STOREID =86"
However, when I pass sqlcmd
to dbGetQuery
, it only returns data with ItemId = 34
, which is the first element in the Id
list.
I'm wondering if anyone has any ideas on why does this happen? Any help would be appreciated!
回答1:
Since I believe R DBI drivers have not yet implemented multiple SQL statements support, dbGetQuery
only returns first statement.
Hence, you need to iteratively run your sqlcmd for multiple SQL statements such as with lapply
to return a list of dataframes, followed by an rbind
call for single master dataframe:
Id <- c(34, 22, 86)
sqlcmd <- paste("select col1, col2 from DB where ItemId =", Id, sep="")
# LIST OF DATAFRAMES
df_list <- lapply(sqlcmd , function(x) dbGetQuery(conn, x))
# FINAL DATAFRAME
final_df <- do.call(rbind, df_list)
Alternatively, use a UNION
or UNION ALL
for one SQL statement.
Id <- c(34, 22, 86)
sqlcmd <- paste("select col1, col2 from DB where ItemId =", Id, sep="")
single_sql <- paste(sqlcmd, collapse = " UNION ")
final_df <- dbGetQuery(conn, single_sql)
Or still use OR
:
single_sql <- paste("select col1, col2 from DB where ItemId =",
paste(Id, collapse=" OR ItemId = "))
final_df <- dbGetQuery(conn, single_sql)
回答2:
I would normally do this like this:
Id1 <- c(34, 22, 86)
Id2 <- paste(Id1, collapse = ", ")
sqlcmd <- paste("select col1, col2 from DB where ItemId in (", Id2, ")", sep="")
Df <- dbGetQuery(conn, sqlcmd)
However if you want to return a list of data frames for each id and run the query three times you could do:
sqlcmd <- paste("select col1, col2 from DB where ItemId in (", Id1, ")", sep="")
dataList <- lapply(sqlcmd, function(x) dbGetQuery(conn, x))
回答3:
This also works with parametrized queries:
library(RSQLite)
conn <- dbConnect(SQLite())
dbWriteTable(conn, "DB", data.frame(col1 = 1, col2 = 2, ItemId = 3))
Id <- c(34, 22, 86)
sqlcmd <- "select col1, col2 from DB where ItemId = ?"
Df <- dbGetQuery(conn, sqlcmd, params = list(Id))
Created on 2018-06-11 by the reprex package (v0.2.0).
Recent versions of DBI and RSQLite will return a data frame that contains the results from these queries concatenated.
来源:https://stackoverflow.com/questions/50767089/r-dbgetquery-with-dynamic-string