【问题标题】:Updating the same record in database based on the for loop基于for循环更新数据库中的相同记录
【发布时间】:2016-12-07 14:20:24
【问题描述】:

我有这种情况,状态为 F 的文档需要保存在表 LOGS 中,并带有文件名。

我的表应该有以下表示:

ID: Incremented
FILE:  fileNameWithOutExt (name of the file)
ERROR: errorCode

但是同一个文件中所有具有相同状态的文档,都需要插入到同一个记录中:

ID: 1
FILE:test
ERROR: id:5,id:9,id:10

我的 if 状态语句位于一个 for 循环中,该循环传递 xml 文件中的所有子项。状态为 F 的需要连接在同一条记录中。

if(status.equals("F")){
    elemValue = element.getChild("id").getValue();
    String fileNameWithExt = f.getName();
    String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
    saveLogs(fileNameWithOutExt, elemValue);
}

private void saveLogs(String fileNameWithOutExt, String elemValue){
    String errorCode = "id:"+ elemValue;
    String query = "INSERT INTO LOGS (FILE,ERROR)VALUES ('"+fileNameWithOutExt+"','"+errorCode+"')";
    String content = "";
    content = SqlTool.selectOneString("DB", query);   
}

【问题讨论】:

    标签: java sql xml


    【解决方案1】:

    您不能仅使用插入指令进行更新。 尝试查看更新指令,简单的方法是使用如下 SQL 脚本:

    UPDATE MyTable SET FieldA=@FieldA WHERE Key=@Key
    
    IF @@ROWCOUNT = 0
        INSERT INTO MyTable (FieldA) VALUES (@FieldA)
    

    【讨论】:

      猜你喜欢
      • 2010-10-08
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 2019-07-18
      相关资源
      最近更新 更多