Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

sql - Java SQLite : no such column error

I'm writing a Java app which has to add a record to a database. Everything works fine until I want to add a local variable to the database (I think I'm placing my parentheses wrong or something). Anyway, I'm tired of looking for the problem and would appreciate some help.

My code:

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) {
    }
}

The error:

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)

Thanks, Tom

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is the in query. The userName variable is not enclosed in quotes

Use below code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...