gpt4 book ai didi

java - 缺少数据库( "where": syntax error) 附近

转载 作者:行者123 更新时间:2023-11-30 05:57:45 27 4
gpt4 key购买 nike

我正在尝试更新我的 sqlite3 数据库,但出现此错误。我能够成功地将数据插入同一数据库,但无法更新它。请帮忙。

[SQLITE_ERROR] SQL error or missing database (near "where": syntax error)
at org.sqlite.core.DB.newSQLException(DB.java:909)
at org.sqlite.core.DB.newSQLException(DB.java:921)
at org.sqlite.core.DB.throwex(DB.java:886)
at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
at org.sqlite.core.NativeDB.prepare(NativeDB.java:127)
at org.sqlite.core.DB.prepare(DB.java:227)
at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:45)
at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>

我的代码

btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query = "update students set ID='"+ id.getText() +"' , username='"+ username.getText() + "', password='"+ pass.getText() +"', firstname='"+ fname.getText() +"','"+ lname.getText() +"' WHERE ID='"+ id.getText() +"' ";
PreparedStatement pst = connection.prepareStatement(query);
pst.execute();

JOptionPane.showMessageDialog(null,"data updated successfully");

pst.close();

} catch (Exception e) {
e.printStackTrace();
}
}
});
btnUpdate.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnUpdate.setBounds(199, 246, 126, 43);
contentPane.add(btnUpdate);

最佳答案

错误是您在没有字段的情况下添加了姓氏值:

... +"','"+ lname.getText() +"' ...

您在值之前错过了 lastname 参数,例如:

... +"', lastname='"+ lname.getText() +"' ...

变成了

String query = "update students set ID='"+ id.getText() +"' , username='"+ username.getText() + "', password='"+ pass.getText() +"', firstname='"+ fname.getText() +"', lastname='"+ lname.getText() +"' WHERE ID='"+ id.getText() +"'  ";

关于java - 缺少数据库( "where": syntax error) 附近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52874176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com