android - OrmLite SQLiteException : no such table

标签 android sqlite ormlite

我将以下 DatabaseHelper 与 OrmLite 一起使用在 Android 上:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String TAG = "databaseHelper";

    private static final String DATABASE_NAME = "mydb.db";

    // Mind onUpgrade when changing this!
    private static final int DATABASE_VERSION = 18;

    private Dao<Account, Integer> accountDao;


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, Account.class);

        } catch (SQLException e) {
            ExceptionHandler.handleException(e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {

    }

    private Dao<Account, Integer> getAccountDao() {
        if (accountDao == null) {
            try {
                accountDao = getDao(Account.class);
            } catch (Exception exc) {
                Log.e(TAG, exc.toString());
                ExceptionHandler.handleException(exc);
            }
        }

        return accountDao;
    }


    public void writeAccount(Account account) {

        try {
            TableUtils.createTableIfNotExists(connectionSource, IWAccount.class);
            getAccountDao().createOrUpdate(account);

        } catch (SQLException exc) {
            Log.e(TAG, exc.toString());
            ExceptionHandler.handleException(exc);
        }

    }

    public void deleteIWAccount() {
        try {
            TableUtils.clearTable(connectionSource, Account.class);
        } catch (SQLException e) {
            Log.e(TAG, e.toString());
            ExceptionHandler.handleException(e);
            e.printStackTrace();
        }
    }

    public Account getAccount() {

        List<Account> accounts = null;

        try {
            accounts = getAccountDao().queryForAll();

        } catch (SQLException e) {
            e.printStackTrace();
            ExceptionHandler.handleException(e);
        }

        if (accounts == null || accounts.isEmpty()) {
            return null;
        }

        if (accounts.size() > 1) {
            ExceptionHandler.handleException(new IllegalStateException("More than 1 IWAccounts in DB"));
        }

        return accounts.get(0);
    }
}

处理的异常都写入Crittercism .

对于少量但不可忽视的用户,发生以下异常:

java.sql.SQLException: Problems executing Android query: SELECT * FROM `account`
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
[...]
Caused by: android.database.sqlite.SQLiteException: no such table: account (code 1): , while compiling: SELECT * FROM `account`

我的 DatabaseHelper 尝试在它的 onCreate() 方法中为 Account 创建表。

我的第一个想法是在 onCreate() 中创建表时出了点问题。 Crittercism 虽然让我浏览发生此错误的用户的所有其他已处理或未处理的异常,并且在创建表期间他们都没有任何异常。

关于这里可能出现的问题有什么想法吗?

编辑:这是我的 DatabaseHelper 的简化版本,其他 Dao 和表也会出现同样的错误。使用的类相当简单,这里是 Account 类:

public class Account implements Serializable {

    //    id is set so we always update the old object instead of creating a new one in the db helper
    @DatabaseField(id = true, canBeNull = false)
    private int mid = 0;

    @DatabaseField
    private String id;
    @DatabaseField
    private String userName;
    @DatabaseField
    private String displayName;

}

EDIT2: 我使用用 @DatabaseTable 注释的持久类对应用程序进行了更新,并在 onUpgrade 中重新创建(或尝试)表(),但问题依旧。

最佳答案

据我所知,您在 Account 类中缺少一个 @DatabaseTable 注释。

来自docs :

Annotation that marks a class to be stored in the database. [...] You specify this annotation above the classes that you want to persist to the database.

下面应该创建一个表 account:

@DatabaseTable
public class Account implements Serializable {

    //    id is set so we always update the old object instead of creating a new one in the db helper
    @DatabaseField(id = true, canBeNull = false)
    private int mid = 0;

    @DatabaseField
    private String id;
    @DatabaseField
    private String userName;
    @DatabaseField
    private String displayName;

}

您可以使用注释中的 tableName 字段更改表名,例如@DatabaseTable(tableName = "accounts") 否则文档状态:

If not set then the name is taken from the class name lowercased.

关于android - OrmLite SQLiteException : no such table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31432243/

相关文章:

python - 运行迁移时检测到表,但未在数据库中创建

android - ORMLite 使用 SQL 插入日期

Android 日志,什么是冗长?

android - fatal error : openssl/evp. h 没有这样的文件或目录

php - WordPress php sqlite 无法连接到 .db

mysql - 使用 peewee 连接 mysql 以及访问表的一些问题

android - ORMLite无法在Android中使用唯一的组合创建行

android - 设置 Gradle 以在 Android Studio 中运行 Java 可执行文件

带有 ActionBar 的 Android 状态栏覆盖

android - 为什么 KeyPair.getPrivate().getEncoded() 为空?