OrmLite SQLiteException: no such table

I am 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);
    }
}

Processed exceptions are written to Crittercism .

For a small but not neglected 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 is trying to create a table for Accountin it onCreate().

My first thought was onCreate()that something went wrong when creating the table . The criterion, although let me see all the other handled or unhandled exceptions for users where this error occurs, and none of them had any exceptions when creating the table.

Any ideas on what might be the issue here?

EDIT. 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(), .

+4
2

, , @DatabaseTable Account.

docs:

, , . [...] , .

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") :

, .

+2

, . @DatabaseTable (tableName = "account" )

-2

Source: https://habr.com/ru/post/1598041/


All Articles