使用粘贴功能将 R 数据框带入 sql 可用列表
Posted
技术标签:
【中文标题】使用粘贴功能将 R 数据框带入 sql 可用列表【英文标题】:Bringing a R dataframe into a sql usable list with paste function 【发布时间】:2017-12-21 06:34:23 【问题描述】:我在 R(Rdataframe) 中有一个数据框/列表,我想直接在 RODBC 查询中使用它,例如
Rdataframe= c('123456','234561','678912')
a= sqlQuery(connection, "Select * from table A where A.Id in Rdataframe")
并且查询必须是这样的,即我不能先在 R 中拉表然后再进行查找
所以我认为它只有在以
等格式出现时才能运行a= sqlQuery(connection, "Select * from table A where A.Id in ('123456','234561','678912'))
但尽管多次尝试 sprintf & paste 我仍然没有成功。
这是我试图尝试但失败的方法
attempt1= sqlQuery(connection, sprintf("Select * from table A where A.Id in %s", Rdataframe))
attempt2=paste(Rdataframe, sep=",")
然后在查询中使用此尝试 2 结构。
每个帮助都很重要
【问题讨论】:
能否请您发布您尝试生成的字符串? 【参考方案1】:Rdataframe= c('123456' , '234561' , '678912')
df_str = paste(Rdataframe , collapse = "','" , sep=" ")
queryStr = paste("Select * from table A where A.Id in ('" ,df_str , "')" , sep="")
print(queryStr)
给出输出
[1] "Select * from table A where A.Id in ('123456','234561','678912')"
【讨论】:
以上是关于使用粘贴功能将 R 数据框带入 sql 可用列表的主要内容,如果未能解决你的问题,请参考以下文章