mercredi 15 juillet 2015

OrmLite SQLiteException: no such table

I'm using the following DatabaseHelper with 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);
    }
}

Handled exceptions are all written to Crittercism.

For a small but not neglectible 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 the table for Account in it's onCreate() method.

My first thought was that something went wrong when creating the table in onCreate(). Crittercism though let's me browse all other handled or unhandled exceptions for the users where this error occurs and none of them had any exceptions during the creation of the table.

Any ideas on what could be the problem here?

EDIT: This is a simplified version of my DatabaseHelper, the same error occurs with other Daos and tables. The classes that are used are rather simple, here's 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;

}

Aucun commentaire:

Enregistrer un commentaire