通过R更新MySql表的问题

时间:2018-10-08 17:43:46

标签: mysql r

给定代码有什么问题。

dbExecute(con_pratham,paste("Update unit_dummy  set isDeleted=",paste(data_to_update$IsDeleted)," , status =", paste0(data_to_update$status), "where UnitId =",paste(data_to_update$UnitId)))

它应该根据其UnitId更新两列“状态” IsDeleted 。此处“ data_to_update”是具有500个观察值的数据帧。这些行中的每一行都有“ Status”和“ IsDeleted”以及“ UnitId”。 当我只打印上面的代码时,它完美地产生了大约500个与此类似的更新SQL语法。

[476] "Update unit_dummy  set isDeleted= 0  , status = 1 where UnitId = 56061"
[477] "Update unit_dummy  set isDeleted= 0  , status = 1 where UnitId = 56063"
[478] "Update unit_dummy  set isDeleted= 0  , status = 1 where UnitId = 56065"
[479] "Update unit_dummy  set isDeleted= 1  , status = 0 where UnitId = 56069"
[480] "Update unit_dummy  set isDeleted= 1  , status = 0 where UnitId = 56070"
[481] "Update unit_dummy  set isDeleted= 1  , status = 1 where UnitId = 56095"
[482] "Update unit_dummy  set isDeleted= 0  , status = 1 where UnitId = 56100"
[483] "Update unit_dummy  set isDeleted= 1  , status = 0 where UnitId = 56132"

当我只复制这些sql行中的任何一条并在服务器上运行时,它可以正常运行,但不能通过R运行。 请为此指导我。.

1 个答案:

答案 0 :(得分:0)

我会尝试这样的事情:

sqls <- paste("Update unit_dummy  set isDeleted=",paste(data_to_update$IsDeleted)," , status =", 
  paste0(data_to_update$status), "where UnitId =",paste(data_to_update$UnitId))


for (sql in sqls){
    dbExecute(con_pratham, sql)
}