创建具有预填充信息的表时,SQLITE数据库错误(表有3列,但提供了2个值)

时间:2018-10-02 18:39:15

标签: java sqlite

所以我只是使用Eclipse进入Java。我已经用它编写了一些Minecraft插件,但这是我为本地社区中心编写的第一个实际桌面程序。

该程序需要使用数据库,因此我将使用本地数据库,而不是基于云的数据库,因为我只是进入数据库。

我已经在数据库上工作了一段时间,但似乎无法克服以下错误:SQL错误或数据库丢失(表用户有3列,但提供了2个值)

下面是我的代码和错误的屏幕截图。

预先感谢, 〜石头

Error Message

Code that I believe the error is coming from

下面的整个类:

package main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLiteConnect {

private static Connection con;
private static boolean hasData = false;

public ResultSet displayUsers() throws ClassNotFoundException, SQLException {
    if (con == null) {
        getConnection();
    }

    Statement state = con.createStatement();
    ResultSet res = state.executeQuery("SELECT firstname, lastname FROM user");
    return res;
}

private void getConnection() throws ClassNotFoundException, SQLException {
    Class.forName("org.sqlite.JDBC");
    con = DriverManager.getConnection("jdbc:sqlite:SQLiteConnect.db");
    initialise();
}

private void initialise() throws SQLException {

    if ( !hasData) {
        hasData = true;

        Statement state = con.createStatement();
        ResultSet res = state.executeQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='user'");

        if ( !res.next() ) {
            System.out.println("Building the User table with prepopulated values.");
            //need to build the table before you can call it if it doesn't already exist
            Statement state2 = con.createStatement();
            state2.execute("CREATE TABLE user(id integer," + "firstname varchar(60)," + "lastname varchar(60)," + "primary key(id));");

            // insert sample data
            PreparedStatement prep = con.prepareStatement("INSERT INTO user values(?,?);");
            prep.setString(1,  "John");
            prep.setString(2, "McNeil");
            prep.execute();

            PreparedStatement prep2 = con.prepareStatement("INSERT INTO user values(?,?);");
            prep2.setString(1,  "Paul");
            prep2.setString(2,  "Smith");
            prep2.execute();

        }

    }
}

public void addUser (String firstname, String lastname) throws ClassNotFoundException, SQLException {

    if (con == null) {
        getConnection();
    }

    PreparedStatement prep = con.prepareStatement("INSERT INTO user values(?,?);");
    prep.setString(1,  firstname);
    prep.setString(2,  lastname);
    prep.execute();

}

}

----------编辑1 ---------

好,所以我将代码从第43行更改为以下内容:

state2.execute("CREATE TABLE user(id INTEGER AUTOINCREMENT," + "firstname varchar(60)," + "lastname varchar(60)," + "primary key(id));");

现在我收到此错误:“ SQL错误或缺少数据库(在“ AUTOINCREMENT”附近:语法错误)”

我很可能只是没有以正确的顺序或类似的方式键入

下图:

Error #2

2 个答案:

答案 0 :(得分:1)

该错误实际上说明了一切-该表具有三列,但在插入时仅提供其中两列。如果要这样做,则需要明确列出要插入的列。

此外,请注意,您无需创建其他PreparedStatement,但可以重复使用同一内容:

PreparedStatement prep = 
    con.prepareStatement("INSERT INTO user (firstname, lastname) VALUES (?, ?;");

prep.setString(1, "John");
prep.setString(2, "McNeil");
prep.executeUpdate();

prep.setString(1, "Paul");
prep.setString(2, "Smith");
prep.executeUpdate();

答案 1 :(得分:0)

如上所述,由于您要插入具有三列的表,但存储过程仅提供两个值,所以会出现该错误。

您希望数据库为您提供id值。如果您设置表来执行此操作,则数据库可以做到这一点,并且存储过程也可以正常工作。您需要告诉数据库在USER表上自动递增(在ID值上使用内部 rowid )。通过在ID列的数据类型中添加INTEGER PRIMARY KEY或INTEGER AUTOINCREMENT,可以将其作为数据库表定义的一部分来完成。

state2.execute("CREATE TABLE user(id integer," + "firstname varchar(60)," + "lastname varchar(60)," + "primary key(id));");

应该是

state2.execute("CREATE TABLE user(id INTEGER PRIMARY KEY," + "firstname varchar(60)," + "lastname varchar(60)," + "primary key(id));");

OR

state2.execute("CREATE TABLE user(id INTEGER AUTOINCREMENT," + "firstname varchar(60)," + "lastname varchar(60)," + "primary key(id));");