android – OrmLite SQLiteException:没有这样的表

我在Android上使用以下DatabaseHelper和OrmLite

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()中创建表时出错了.批评虽然让我浏览发生此错误的用户的所有其他已处理或未处理的异常,并且在创建表时没有任何异常.

关于这可能是什么问题的任何想法?

编辑:这是我的DatabaseHelper的简化版本,与其他Daos和表相同的错误.使用的类非常简单,这里是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.

以下应创建一个表帐户:

@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.

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能