gpt4 book ai didi

java - ORMLite SQLite w/Plain Java 错误 : "SQL error or missing database"

转载 作者:行者123 更新时间:2023-12-02 06:11:56 24 4
gpt4 key购买 nike

我已经在 Android 上的几个不同项目中使用 ORMLite 几个月了。太棒了!有一天,我开始向后移植我的一个项目,该项目有一个普通的旧 java 组件,我可以解析一些数据,并创建一个 sqlite 数据库,并将其包含在 android 项目中。

我一直在使用 jdbc 库来调用普通的 sqlite 语句,当我使用 xerial 的 jdbc 库切换到 ormlite-jdbc 时,当我尝试在新的数据库文件中创建表时,我的代码立即抛出错误:

Exception in thread "main" com.test.library.java.exception.BAException: java.sql.SQLException: SQL statement failed: CREATE TABLE IF NOT EXISTS `chapter` (`book` VARCHAR NOT NULL , `chapter` INTEGER , `chapter` VARCHAR NOT NULL , `_id` INTEGER PRIMARY KEY AUTOINCREMENT ) 
at com.test.parser.Database.createTables(Database.java:109)
at com.test.parser.Database.<init>(Database.java:81)
at com.test.parser.runnable.TranslationParserRunnable.run(TranslationParserRunnable.java:35)
at com.test.parser.Parser.parse(Parser.java:13)
at com.test.parser.Main.main(Main.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.sql.SQLException: SQL statement failed: CREATE TABLE IF NOT EXISTS `chapter` (`book` VARCHAR NOT NULL , `chapter` INTEGER , `chapter` VARCHAR NOT NULL , `_id` INTEGER PRIMARY KEY AUTOINCREMENT )
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:468)
at com.j256.ormlite.table.TableUtils.doCreateTable(TableUtils.java:442)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:220)
at com.j256.ormlite.table.TableUtils.createTableIfNotExists(TableUtils.java:61)
at com.test.parser.Database.createTables(Database.java:102)
... 9 more
Caused by: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (duplicate column name: chapter)
at org.sqlite.DB.newSQLException(DB.java:383)
at org.sqlite.DB.newSQLException(DB.java:387)
at org.sqlite.DB.throwex(DB.java:374)
at org.sqlite.NestedDB.prepare(NestedDB.java:134)
at org.sqlite.DB.prepare(DB.java:123)
at org.sqlite.PrepStmt.<init>(PrepStmt.java:42)
at org.sqlite.Conn.prepareStatement(Conn.java:404)
at org.sqlite.Conn.prepareStatement(Conn.java:399)
at com.j256.ormlite.jdbc.JdbcDatabaseConnection.compileStatement(JdbcDatabaseConnection.java:131)
at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:459)
... 13 more

要得到这个错误,我所要做的就是:

public Database(File dbFile) {
try {
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
throw new BAException(e);
}

dbFile.getParentFile().mkdirs();
dbFile.delete();

String connectionName = "jdbc:sqlite:" + dbFile.getPath();
System.out.println("Connection Path: " + connectionName);

try {
mConnectionSource = new JdbcConnectionSource(connectionName);
createTables();
} catch (SQLException e) {
throw new BAException(e);
}

createTables();
}

private void createTables() {
try {
TableUtils.createTableIfNotExists(mConnectionSource, ExampleTable.class);
} catch (SQLException e) {
throw new BAException(e);
}

}

当我尝试创建表时抛出错误。有趣的是,如果我只使用直接 JDBC,一切都会正常工作:

public Database(File dbFile) {
try {
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
throw new BAException(e);
}

dbFile.getParentFile().mkdirs();
dbFile.delete();

String connectionName = "jdbc:sqlite:" + dbFile.getPath();
System.out.println("Connection Path: " + connectionName);

try {
Connection connection = DriverManager.getConnection(connectionName);

Statement statement = null;
try {
statement = connection.createStatement();
statement.setQueryTimeout(30);

statement.executeUpdate(SQL.CREATE_TABLE_EXAMPLE);
statement.executeUpdate("CREATE TABLE \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')");
statement.executeUpdate("INSERT INTO \"android_metadata\" VALUES ('en_US')");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null)
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

} catch (SQLException e) {
throw new BAException(e);
}

同样,这工作得很好,使用 ORMLite 的等效方法会抛出丢失数据库异常。我花了 6 个多小时试图解决这个问题,看来问题出在 ormlite 上。我注意到在运行 mConnectionSource = new JdbcConnectionSource(connectionName); 时创建了一个空数据库文件(至少我认为这是创建它的行),但是我似乎缺乏交互的能力有了它。

我还没有找到任何使用 DAO 和连接源与 sqlite 交互的 ormlite 代码示例。看来所有的 jdbc 示例都是使用 h2 内存数据库完成的。

任何帮助都会很棒。

其他一些信息:我正在使用 gradle 进行构建。

compile 'com.j256.ormlite:ormlite-jdbc:4.48'
compile 'org.xerial:sqlite-jdbc:3.7.2'

最佳答案

immediately my code throws an error when I try to create a table in a fresh database file:

因此,可能的问题可能是您似乎正在尝试写入目录而不是数据库文件:

dbFile.mkdirs();
dbFile.delete();
String connectionName = "jdbc:sqlite:" + dbFile.getPath();
...

也许你的意思是:

dbFile.getParentFile().mkdirs();

如果 dbFile 应该是一个目录,那么您需要在文件后面提供一个名称:

String connectionName = "jdbc:sqlite:" + dbFile.getPath() + "/db.sqlite";

关于java - ORMLite SQLite w/Plain Java 错误 : "SQL error or missing database",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21809185/

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