Ormlite database helper - onCreate () not called

I am using ormlite.android.4.31.jar I have a typical DatabaseHelper

public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_NAME = "realestate.db"; private static final int DATABASE_VERSION = 1; private Dao<TabKraj, Integer> krajDao; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) { try { TableUtils.createTable(connectionSource, TabKraj.class); initData(); } catch (Exception e) { Log.e(DatabaseHelper.class.getName(), "Unable to create datbases", e); } } @Override public void onUpgrade(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource, int oldVer, int newVer) { try { TableUtils.dropTable(connectionSource, TabKraj.class, true); onCreate(sqliteDatabase, connectionSource); } catch (SQLException e) { Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version " + oldVer + " to new " + newVer, e); } } public Dao<TabKraj, Integer> getKrajDao() throws SQLException{ if (krajDao == null) { krajDao = getDao(TabKraj.class); } return krajDao; } private void initData(){ Log.d(Constants.DEBUG_TAG, "data initiating"); TabKraj k1 = new TabKraj(); TabKraj k2 = new TabKraj(); k1.setNazov("Kosicky kraj"); k1.setId(1); try { getKrajDao().create(k1); } catch (SQLException e) { Log.e(Constants.DEBUG_TAG, "Data initialing ERROR"); } } } 

the application is deleted, the data is cleared ... I run the application in debug mode from eclipse, the DatabaseHleper constructor is called, but onCreate() not called.

Where can the problem arise?

+4
source share
2 answers

As @ k-mera said:

The database file will only be created if you have done some operations in the database, such as "insert" .

+3
source

Although you say that the data is cleared, I suspect that Android believes that this is not the case. To completely delete the data, I uninstalled the application and reinstalled it.

Since your onUpgrade calls onCreate , you can also increase the DATABASE_VERSION value, which will drop and re-create the data.

+2
source

All Articles