您好,我想删除mysql表中名为“ kosik”的所有行,并将它们传递给另一个名为“ obj_zoznam”的表。
我在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());
}
但是,此代码始终仅从表“ kosik”中删除一条记录。我试图弄清楚如何删除此表中的所有记录。
谁能告诉我我在做什么错?我将非常感谢。
谢谢。
答案 0 :(得分:1)
我试图弄清楚如何删除此表中的所有记录。
然后从您的where
语句中删除该过滤条件delete
delete from kosik
答案 1 :(得分:1)
从LIMIT 1
中删除SELECT
,以便显示所有记录,而不仅仅是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");
请勿从WHERE
中删除DELETE
,否则您将选择1条记录(stmt
),将其插入另一张表(stmt2
)中,然后清除所有记录将该表插入obj_zoznam
(stmt3
)中之前。
答案 2 :(得分:0)
您将查询的结果限制为一行,因此您始终只有一个ID。 sstr2的执行将始终删除具有第一个id的记录
答案 3 :(得分:0)
如果要删除保留表结构的条目,则应使用truncate命令。
truncate table kosik