繁体   English   中英

带有普通Java错误的ORMLite SQLite:“SQL错误或缺少数据库”

[英]ORMLite SQLite w/ Plain Java Error: “SQL error or missing database”

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

我一直在使用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'

当我尝试在新数据库文件中创建表时,我的代码立即抛出错误:

所以可能的问题可能是你似乎试图写入目录而不是数据库文件:

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

也许你的意思是:

dbFile.getParentFile().mkdirs();

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

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

我有2个同名的列。 修好后,一切正常。 ORMLite很精彩。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM