0

我有一个data.frame存储在SQL数据库中的,用包的dbWriteTable功能创建的R sqldf

data.frame我想知道是否有一种方法可以在此 SQL 数据库中按名称获取特定列。

对于按名称获取行,此代码有效:

#let's say the row names I want to select from the data.frame are in a vector called selected_row_names, where the data.frame is called df and the SQL database is called db
query_text <- paste("('",paste(selected_row_names,collapse="', '"),"')",sep="")
query_expression_text <- paste("select * from ",df," where row_names in ",query_text," order by row_names",sep="")
selected_df = dbGetQuery(db, query_expression_text)

我的问题是按名称选择列是否有任何等价物?

如果我尝试这段代码,我选择的列名是:“col-1T-A_m1”和“col-1T-A_m2”:

query_string <- paste("select col-1T-A_m1, col-1T-A_m1 from ", df, sep="")
selected_df  <- dbGetQuery(db, query_string)

我收到此错误消息:

Error in sqliteSendQuery(con, statement, bind.data) : error in statement: unrecognized token: "1T". 
4

1 回答 1

1

假设您正在运行原始 SQL 查询,那么您应该能够简单地指定您想要的列名SELECT,例如

query_string <- paste("select col1, col2, col3 from ", df)
selected_df  <- dbGetQuery(db, query_string)
于 2016-04-14T16:54:30.417 回答