Android – ormlite sqliteexception: there is no such table

I use the following databasehelper and ormlite on 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);
    }
}

All exceptions handled are written to crittercism

For a small but non negligible number of users, the following exception occurs:

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`

My databasehelper tries to create a table for account in its oncreate() method

My first thought was that there was an error creating the table in oncreate(). Although the criticism allowed me to browse all other handled or unhandled exceptions of the user with this error, and there were no exceptions when creating the table

Any ideas about what this might be?

Edit: This is a simplified version of my databasehelper. The same error as other Daos and tables. The class used is very simple. Here is the account class:

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: I updated the application. My persistence class annotated with @ databasetable and recreated (or tried) the table in onupgrade(), but the problem still exists

resolvent:

As I can see, you are missing the @ databasetable annotation in the account class

From docs:

The following table accounts should be created:

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

}

You can use the tablename field in the comment to change the table name, such as @ databasetable (tablename = "accounts"), otherwise the document status:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>