微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐