mysql - 在 SQL 表中动态插入 R 输出

标签 mysql sql-server r

我几乎没有 SQL 语句的经验,所以我为可能对这个问题的无知表示歉意。但是,假设我有一个 SQL 表 results,其中包含 b1 b2 b3 b4 的列字段,并且我有对应于的 R 输出 dat这些值看起来像:

print(dat)
b1  b2  b3  b4
7   8   7   1

因此我可以运行如下所示的 SQL 语句:

a<-paste("INSERT INTO `results` (`b1`,`b2`,`b3`,`b4`) VALUES ","(",dat$b1,",",dat$b2,",",dat$b3,",",dat$b4",")",";",sep="")
for(i in(1:length(b))){
query(b[i])
}

哪个工作正常;然而,这不是动态的,因为 dat (即 R 输出)并不总是包含 结果 中找到的所有列值(即数据库列字段) ,尽管输出永远不会有在数据库列字段中找不到的列(例如,在这种情况下 dat 永远不会有 b5 列)。我正在尝试动态编写代码,这样我就不必在代码中写出所有 dat 列和 results 列名称字段,并将 dat results 中,这样无论顺序如何,dat 中的列都将进入 results 中相应的列字段,最后如果dat 中缺少列值,NA 将进入 results 中相应的列字段。例如,如果 dat 看起来像:

print(dat)
b4  b1
7   8

结果看起来像:

b1  b2  b3  b4
8   NA  NA  7

谢谢!

最佳答案

您可以以非常简单的方式对其进行参数化(并且您可以将下面的功能包装到一个函数中以方便使用):

dat <- mtcars

inserts <- sprintf("INSERT INTO `%s` (%s) VALUES (%s);",
        "results",
        paste(sprintf("`%s`", colnames(dat)), collapse=", "),
        sapply(1:nrow(dat), function(i) {
          paste(sprintf("`%s`", unlist(dat[i,], use.names=FALSE)) , collapse=", ")
        }))

head(inserts)
## [1] "INSERT INTO `results` (`mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`) VALUES (`21`, `6`, `160`, `110`, `3.9`, `2.62`, `16.46`, `0`, `1`, `4`, `4`);"    
## [2] "INSERT INTO `results` (`mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`) VALUES (`21`, `6`, `160`, `110`, `3.9`, `2.875`, `17.02`, `0`, `1`, `4`, `4`);"   
## [3] "INSERT INTO `results` (`mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`) VALUES (`22.8`, `4`, `108`, `93`, `3.85`, `2.32`, `18.61`, `1`, `1`, `4`, `1`);"  
## [4] "INSERT INTO `results` (`mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`) VALUES (`21.4`, `6`, `258`, `110`, `3.08`, `3.215`, `19.44`, `1`, `0`, `3`, `1`);"
## [5] "INSERT INTO `results` (`mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`) VALUES (`18.7`, `8`, `360`, `175`, `3.15`, `3.44`, `17.02`, `0`, `0`, `3`, `2`);" 
## [6] "INSERT INTO `results` (`mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`) VALUES (`18.1`, `6`, `225`, `105`, `2.76`, `3.46`, `20.22`, `1`, `0`, `3`, `1`);"
dat <- iris

inserts <- sprintf("INSERT INTO `%s` (%s) VALUES (%s);",
        "results",
        paste(sprintf("`%s`", colnames(dat)), collapse=", "),
        sapply(1:nrow(dat), function(i) {
          paste(sprintf("`%s`", unlist(dat[i,], use.names=FALSE)) , collapse=", ")
        }))

head(inserts)
## [1] "INSERT INTO `results` (`Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`) VALUES (`5.1`, `3.5`, `1.4`, `0.2`, `1`);"
## [2] "INSERT INTO `results` (`Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`) VALUES (`4.9`, `3`, `1.4`, `0.2`, `1`);"  
## [3] "INSERT INTO `results` (`Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`) VALUES (`4.7`, `3.2`, `1.3`, `0.2`, `1`);"
## [4] "INSERT INTO `results` (`Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`) VALUES (`4.6`, `3.1`, `1.5`, `0.2`, `1`);"
## [5] "INSERT INTO `results` (`Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`) VALUES (`5`, `3.6`, `1.4`, `0.2`, `1`);"  
## [6] "INSERT INTO `results` (`Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`) VALUES (`5.4`, `3.9`, `1.7`, `0.4`, `1`);"

set.seed(1492)
dat <- data.frame(b1=sample(10, 10),
                  b2=sample(10, 10),
                  b3=sample(10, 10),
                  b4=sample(10, 10))

inserts <- sprintf("INSERT INTO `%s` (%s) VALUES (%s);",
        "results",
        paste(sprintf("`%s`", colnames(dat)), collapse=", "),
        sapply(1:nrow(dat), function(i) {
          paste(sprintf("`%s`", unlist(dat[i,], use.names=FALSE)) , collapse=", ")
        }))

head(inserts)
## [1] "INSERT INTO `results` (`b1`, `b2`, `b3`, `b4`) VALUES (`3`, `7`, `7`, `2`);" 
## [2] "INSERT INTO `results` (`b1`, `b2`, `b3`, `b4`) VALUES (`2`, `6`, `4`, `9`);" 
## [3] "INSERT INTO `results` (`b1`, `b2`, `b3`, `b4`) VALUES (`9`, `2`, `2`, `7`);" 
## [4] "INSERT INTO `results` (`b1`, `b2`, `b3`, `b4`) VALUES (`1`, `4`, `5`, `10`);"
## [5] "INSERT INTO `results` (`b1`, `b2`, `b3`, `b4`) VALUES (`7`, `10`, `1`, `6`);"
## [6] "INSERT INTO `results` (`b1`, `b2`, `b3`, `b4`) VALUES (`6`, `9`, `10`, `4`);"

但是,如果我们更了解您真正想要解决的问题,可能会有更优化的方法将这些数据推回数据库。

关于mysql - 在 SQL 表中动态插入 R 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570060/

相关文章:

php - 有没有办法搜索以逗号分隔的 json 属性并通过不同的属性名称返回成员 id?

mysql - 亚马逊 EC2 : MySql Database storage?

mysql - 无法使用 Sequelize 库连接三个具有 Sum 聚合函数的表

SQL Server 存储过程在进行微小更改后需要很长时间才能运行

r - 使用其他列中的值跨多个列进行条件变异 - 在 tidyverse 中寻找有效的解决方案

mysql - 使用联接时 GROUP BY 未正确排序

.net - 如何使用 Entity Framework 6.1 正确标记标识列?

sql - 如何对 SQL 表列进行验证?

r - 从另一个包中调用时,无法从 mgcv 访问 "ldTweedie"函数

R ggplot2 : bar chart of a time series