R编程:在db到R中创建表

时间:2020-12-29 10:09:02

I have a matrix called "final_set" which gets created in my R code. Using some of the columns from this final set, I want to create a database table. I'm using the RJDBC package to make this happen. However, for some reason its not recognizing "final_set" . I know my connection is okay because I've tried out other sql queries using that. Could anyone help me with this please?

我有一个名为“final_set”的矩阵,它在我的R代码中创建。使用这个最终集合中的一些列,我想创建一个数据库表。我正在使用RJDBC软件包来实现这一目标。但是,由于某种原因,它不承认“final_set”。我知道我的连接没问题,因为我已经尝试了使用它的其他SQL查询。有人可以帮我这个吗?

 SqlStr = paste("SELECT",
                 S_Vars, ",",     #Global Variable 
                 final_set$pub_idx, ",",
                 final_set$qty_idx
                 into dbo.temp1
                 "from",
                 final_set,
                 sep = ""
               )

  dbSendUpdate(conn,SqlStr)

Error: Error in paste(**): object final_set not found

错误:粘贴错误(**):找不到对象final_set

1 个解决方案

#1


0  

I think you want your query to be:

我想你想要你的查询是:

SqlStr = paste("SELECT",
                S_Vars,     #Global Variable 
                ", pub_idx, qty_idx into dbo.temp1 from final_set")

R objects that are not strings shouldn't go in your query as R objects, as their values (not their names) will be coerced to strings.

不是字符串的R对象不应该作为R对象进入查询,因为它们的值(而不是它们的名称)将被强制转换为字符串。

I'm still a little confused as to your goal. If you're trying to create a table out of an R object, (i.e., final_set is not already a table in your database), then the database won't be able to select columns from final_set. Rather, you should use a function like dbWriteTable. If I'm correct about that, then I think what you're looking for is

我对你的目标仍然有点困惑。如果您尝试使用R对象创建表(即,final_set不是数据库中的表),则数据库将无法从final_set中选择列。相反,您应该使用像dbWriteTable这样的函数。如果我对此是正确的,那么我认为你正在寻找的是

dbWriteTable(conn, "temp1", final_set[, c(S_Vars, "pub_idx", "qty_idx")] 

#1


0  

I think you want your query to be:

我想你想要你的查询是:

SqlStr = paste("SELECT",
                S_Vars,     #Global Variable 
                ", pub_idx, qty_idx into dbo.temp1 from final_set")

R objects that are not strings shouldn't go in your query as R objects, as their values (not their names) will be coerced to strings.

不是字符串的R对象不应该作为R对象进入查询,因为它们的值(而不是它们的名称)将被强制转换为字符串。

I'm still a little confused as to your goal. If you're trying to create a table out of an R object, (i.e., final_set is not already a table in your database), then the database won't be able to select columns from final_set. Rather, you should use a function like dbWriteTable. If I'm correct about that, then I think what you're looking for is

我对你的目标仍然有点困惑。如果您尝试使用R对象创建表(即,final_set不是数据库中的表),则数据库将无法从final_set中选择列。相反,您应该使用像dbWriteTable这样的函数。如果我对此是正确的,那么我认为你正在寻找的是

dbWriteTable(conn, "temp1", final_set[, c(S_Vars, "pub_idx", "qty_idx")]