简体   繁体   English

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

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

I've been using ORMLite for months on Android in several different projects. 在几个不同的项目中,我已经在Android上使用ORMLite数月了。 It's been great! 太棒了! The other day I started backporting a project of mine that has a plain old java component that I parse some data, and create a sqlite database that I include in an android project. 前几天我开始向后移植我的一个项目,该项目有一个普通的旧java组件,我解析一些数据,并创建一个我在android项目中包含的sqlite数据库。

I had been using a jdbc library to call plain sqlite statements, and when I swapped over to ormlite-jdbc with xerial's jdbc library, immediately my code throws an error when I try to create a table in a fresh database file: 我一直在使用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

To get that error, all I would have to do is: 要得到这个错误,我所要做的就是:

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

  }

An error is thrown when I try to create the tables. 我尝试创建表时抛出错误。 The interesting thing is, if I just use straight JDBC everything works fine: 有趣的是,如果我只使用直接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);
}

So again, that works fine, and the equivalent using ORMLite throws a missing database exception. 所以再一次,它工作正常,使用ORMLite的等价物抛出了一个丢失的数据库异常。 I spent 6+ hours trying to figure this out, and it seems like the issue lies in ormlite. 我花了6个多小时试图解决这个问题,似乎问题在于ormlite。 I noticed when running mConnectionSource = new JdbcConnectionSource(connectionName); 我注意到在运行mConnectionSource = new JdbcConnectionSource(connectionName); there is an empty database file created (at least I assume this is the line that creates it), but then I seem to lack the ability to interact with it. 有一个空数据库文件创建(至少我假设这是创建它的行),但后来我似乎缺乏与它交互的能力。

I haven't been able to find any ormlite code examples interacting with sqlite using DAO's and connection sources. 我无法找到任何使用DAO和连接源与sqlite交互的ormlite代码示例。 It seems all the jdbc examples are done using an h2 memory database. 似乎所有的jdbc示例都是使用h2内存数据库完成的。

Any help would be wonderful. 任何帮助都会很精彩。

Some other information: I'm building with gradle. 其他一些信息:我正在建立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: 当我尝试在新数据库文件中创建表时,我的代码立即抛出错误:

So on possible problem may be that you seem to be trying to write to a directory instead of a database file: 所以可能的问题可能是你似乎试图写入目录而不是数据库文件:

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

Maybe you meant: 也许你的意思是:

dbFile.getParentFile().mkdirs();

If dbFile is supposed to be a directory then you need to provide a name after the file: 如果dbFile应该是一个目录,那么你需要在文件后面提供一个名称:

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

I had 2 columns with the same name. 我有2个同名的列。 After fixing that, everything worked fine. 修好后,一切正常。 ORMLite is wonderful. ORMLite很精彩。

暂无
暂无

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

相关问题 Java和SQLite [SQLITE_ERROR] SQL错误或缺少数据库() - Java and SQLite [SQLITE_ERROR] SQL error or missing database () java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(“ ID”附近:语法错误) - java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near “ID”:syntax error) java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(“名称”附近:语法错误) - java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near “Name”: syntax error) java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(接近“=”:语法错误) - java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near “=”:syntax error) 尝试从SQLite数据库更新数据时出现SQL错误或数据库丢失(在“。”附近:语法错误)的Java错误 - SQL error or missing database (near “.”: syntax error) error in java when trying to update data from sqlite databse Java [SQLITE_ERROR] SQL 错误或缺少数据库(“resi”附近:语法错误) - Java [SQLITE_ERROR] SQL error or missing database (near “resi”: syntax error) SQLITE_ERROR SQL错误或数据库丢失(“(”附近:语法错误) - SQLITE_ERROR SQL error or missing database (near “(”: syntax error) BlackBerry上的SQLite提供SQL逻辑错误或缺少数据库错误 - SQLite on BlackBerry giving SQL logic error or missing database error SQL 错误或缺少数据库(“:01”附近:语法错误)在 SQLite - SQL error or missing database (near “:01”: syntax error) in SQLite 将SQLite数据库添加到jar时出现SQL错误或缺少数据库 - Sql error or missing database when adding sqlite database to jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM