gpt4 book ai didi

Java SQLite : no such column error

转载 作者:IT王子 更新时间:2023-10-29 06:30:19 26 4
gpt4 key购买 nike

我正在编写一个必须向数据库添加记录的 Java 应用程序。一切正常,直到我想向数据库添加一个局部变量(我想我把括号放错了或什么的)。不管怎样,我已经厌倦了寻找问题,希望能得到一些帮助。

我的代码:

public void newUser(int userID, String userName, String credentials) {
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager
.getConnection("jdbc:sqlite:c:/temp/alarmsystem.db");
Statement statement = conn.createStatement();
statement.execute("insert into Users values(" + 1 + "," + userName
+ "," + "'Helloskit'" + ") ");
core.printToConsole("created");
ResultSet rs = statement.executeQuery("select * from Users");

while (rs.next()) {
String s = rs.getString("Username");

core.printToConsole("name = " + s);

}
} catch (Exception e) {
}
}

错误:

java.sql.SQLException: no such column: Tombellens
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NestedDB.prepare(NestedDB.java:115)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.execute(Stmt.java:82)
at me.server.DBCommunications.DBConnection.newUser(DBConnection.java:59)
at me.server.Core.Core.newUser(Core.java:61)
at me.server.LocalUser.Console.main(Console.java:72)

谢谢,汤姆

最佳答案

问题是 in 查询。 userName 变量未用引号引起来

使用下面的代码:

statement.execute("insert into Users values(" + 1 + ",'"  +  userName  + "',"  + "'Helloskit'" +") ");

关于Java SQLite : no such column error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344846/

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