Java SQLite:没有这样的列错误

时间:2012-02-18 22:40:11

标签: java sql sqlite

我正在编写一个必须将记录添加到数据库的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)

谢谢,汤姆

2 个答案:

答案 0 :(得分:17)

问题在于查询。 userName变量未包含在引号

使用以下代码:

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

答案 1 :(得分:1)

在您的SQL语句中,变量userName应该用引号括起来。