在 sql2o 中插入时出错

Error while inserting in sql2o

我想创建 table 并向其中插入一些值。我正在尝试在以下代码中使用 H2 数据库和 sql2o 框架来完成此操作:

public class Main {

    private static final String DB_DRIVER = "org.h2.Driver";
    private static final String DB_CONNECTION = "jdbc:h2:~/test";
    private static final String DB_USER = "";
    private static final String DB_PASSWORD = "";
    static String TABLE = "PERSONS";
    static Sql2o sql2o;

    public static void main(String[] args) throws Exception, SQLException {
        sql2o = new Sql2o(DB_CONNECTION, DB_USER, DB_PASSWORD);
        createTable();
        insertIntoTable();
    }

    public static void createTable() {
        final String tableSql = "CREATE TABLE IF NOT EXISTS " + TABLE + " (id int, name varchar(255))";
        try (org.sql2o.Connection con = sql2o.beginTransaction()) {
            con.createQuery(tableSql).executeUpdate();
            con.commit();
            con.close();
        }
    }

    public static void insertIntoTable() {
        String insertSql = "insert into " + TABLE + " values (:id, :name)";
        try (org.sql2o.Connection con = sql2o.open()) {
            con.createQuery(insertSql).addParameter("id", 1).addParameter("name", "test").executeUpdate();
            con.close();
        }
    }
}

毕竟我得到一个错误:

Error preparing statement - Column count does not match; SQL statement: insert into PERSONS values (?, ?) [21002-191]

SQL的说法是正确的,列的名称和类型相互匹配,我实在想不明白是什么问题。

现有的 PERSONS table 与 CREATE TABLE IF NOT EXISTS 语句中的结构不匹配。删除并重新创建 table 解决了问题。