我无法删除 mysql table 中的所有行

I'm not able to delete all rows in mysql table

您好,我想删除名为 "kosik" 的 mysql table 中的所有行,并将它们传递给另一个名为 "obj_zoznam" 的 table。

我在 java 中使用此代码:

String orderstatus = "Accepted";
        try {
            stmt = con.createStatement();
            stmt2 = con.createStatement();
            stmt3 = con.createStatement();

            rs = stmt.executeQuery("select ID as idzaz, ID_pouzivatela as idpou, ID_tovaru as idtov, cena as suma, ks as kusy from kosik order by ID limit 1");

            while (rs.next()) {

                int zaz = rs.getInt("idzaz");
                int pou = rs.getInt("idpou");
                int tov = rs.getInt("idtov");
                int suma = rs.getInt("suma");
                int kusy = rs.getInt("kusy");

                String sstr = "insert into obj_zoznam (ID_pouzivatela, ID_tovaru, suma, ks, stav) values ("
                        + "'" + id_usera + "', "
                        + "'" + tov + "', "
                        + "'" + suma + "', "
                        + "'" + kusy + "', "
                        + "'" + orderstatus + "')";

                String sstr2 = "delete from kosik where ID = " + zaz;

                stmt2.executeUpdate(sstr);
                stmt3.executeUpdate(sstr2);
            }

            stmt.close();
            stmt2.close();
            stmt3.close();
        } catch (Exception e) {
            out.println("Error in writing: " + e.toString());
        }

但是此代码总是只从 table "kosik" 中删除一条记录。我想知道如何删除此 table.

中的所有记录

谁能告诉我我做错了什么?我将不胜感激。

谢谢。

I'm trying to figure how to delete all records in this table.

然后从您的 delete 语句中删除该过滤器 where 条件

delete from kosik

从您的 SELECT 中删除 LIMIT 1,这样您就可以调出所有记录,而不仅仅是 1 条。这一行:

rs = stmt.executeQuery("select ID as idzaz, ID_pouzivatela as idpou, ID_tovaru as idtov, cena as suma, ks as kusy from kosik order by ID limit 1");

取消限制1:

rs = stmt.executeQuery("select ID as idzaz, ID_pouzivatela as idpou, ID_tovaru as idtov, cena as suma, ks as kusy from kosik order by ID");

请勿从 DELETE 中删除 WHERE 否则您将 select 1 条记录 (stmt),将其插入另一条 table ( stmt2) 然后清除所有 table,然后再将所有其他记录插入 obj_zoznam (stmt3).

您将查询结果限制为一行,因此您始终只有一个 ID。 sstr2 的执行将始终删除第一个 id

的记录

如果要删除保留 table 结构的条目,使用 truncate 命令应该可以。

truncate table kosik