【问题标题】:Repetition in updating rows in MySQL table重复更新 MySQL 表中的行
【发布时间】:2012-09-24 04:53:34
【问题描述】:

我正在尝试更新已经存在的 MySQL 表中的列。这些值必须从文本文件中读取,然后必须插入到指定的列中。

我尝试了以下方法:

int counter=0;
        while((fileLine=in.readLine())!=null) 
        { 
            System.out.println("Line read is: "+fileLine);

                //execute db insertion
                try {
                    //insert in the database
                    Query= "update db.table set col4=?";    //database
                    preparedStmt3 = DBConnection.con.prepareStatement(Query); 
                    preparedStmt3.setString (1, fileLine);
                    preparedStmt3.executeUpdate();
                    System.out.println("Complete update statement for row: "+counter);
                    counter++;
                } catch (Exception e) {
                    System.out.println("DB_Error:_"+ e.toString());
                }

        } //end while loop 

我试图输出fileLine 值,它似乎是正确的,并且在每一轮循环中都会正确更改。但是,在我运行程序并检查数据库之后,我发现插入的值只是最后一行(并且这在所有记录中重复,这不是我应该做的,因此在记录中插入每一行) .这个问题的原因是什么?

编辑: 文本文件包含以下几行: 啊啊啊 bbb ccc ddd eee fff

我添加了一些printline 语句以查看调试后的运行输出如下:

而数据库只包含插入的最后一行

【问题讨论】:

  • 您的列似乎与表中的其余列无关?那么你可能应该分开到不同的表。

标签: java mysql


【解决方案1】:

您的 SQL 没有添加 WHERE 子句来指定要更新的行。因此,在每次迭代中,列中的所有记录都将其col4 字段更新为值。最后一个是剩下的更新。

【讨论】:

  • 我没有任何条件。我想更新所有记录。我的文件有行 = 记录数。我需要从第一个到最后一个记录中插入每一行值。我有作为主键和自动增量的 id 列,但我删除了许多值,因此 id 号不连续。我宁愿不提出任何条件。这可能吗?
  • 请告诉我如何自动更新所有行。假设 row1 的 id=1,row2 的 id=2,.. 等等。我怎样才能在查询中自动化呢??
  • 添加到 SQL ` where id=?`,并使用您在每次迭代中增加的计数器变量设置此参数。
【解决方案2】:

试试这个,

Connection conn = ConnectionBean.getInstance().getDBConnection();
int counter=0;
String fileLine;
BufferedReader in;
String query1 = "select id from test"; // get all the id's from the table you wanna do  the update
ArrayList<Integer> al = new ArrayList<Integer>(); // store them in an arraylist
try {
PreparedStatement stmnt = conn.prepareStatement(query1);
ResultSet rs = stmnt.executeQuery();
while(rs.next()) {
    al.add(rs.getInt("id"));
}
}
catch(Exception ex) {
    ex.printStackTrace();
}

try {
    in = new BufferedReader(new FileReader("file1.txt"));
    int h=1;
    while((fileLine=in.readLine())!=null && al.size()>0)  
    {   

        String query="UPDATE chaitu.test SET col=? WHERE id="+h ;
        PreparedStatement statement = conn.prepareStatement(query);

        statement.setString(1, fileLine);
        statement.executeUpdate();
        h++;
    } //end while loo


} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    }

【讨论】:

  • 但是我不知道id。我希望对所有记录进行更新。从第一个到最后一个,不指定任何数字。我的id是自动递增的,我删除了很多记录,所以数字不连续。
  • 仍然没有任何改变。只有从文件中读取的最后一行是插入到所有记录中的那一行。
  • @JuryA 如果您发布数据库屏幕截图会很棒。
  • chaitanya10:它不起作用。我需要在col4中插入的是:在记录1中我需要:“aaa”,在记录2中:“bbb”,在记录3中:“ccc”......等等。
  • @JuryA 如果您想在 row1 的 pk_hex 中插入“aaa”,在 row2 的 pk_Hex 中插入“bbb”,那么您必须在更新中指定 where 子句。没有它(在您的代码中)col pk_hex 的所有字段行都将使用“fff”更新。您应该确定提供 where 条款
【解决方案3】:

您正在使用更新查询来插入值。使用

INSERT INTO db.table (col4) VALUES (?)

【讨论】:

  • 这不起作用。它将插入新记录。我需要将从文本文件中读取的值插入到已经可用的记录中。
猜你喜欢
  • 1970-01-01
  • 2012-12-22
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
相关资源
最近更新 更多