简体   繁体   English

Ormlite数据库助手 - 未调用onCreate()

[英]Ormlite database helper - onCreate() not called

I am using ormlite.android.4.31.jar I have typical DatabaseHelper 我正在使用ormlite.android.4.31.jar我有典型的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");
        }
    }
}

app is uninstalled, data cleared ... I am running app in debug mode from eclipse, constructor of DatabaseHleper is called but onCreate() is not called. 应用程序已卸载,数据已清除...我正在从eclipse以调试模式运行应用程序,调用DatabaseHleper构造函数但未调用onCreate()

Where could the problem be? 问题出在哪里?

As @k-mera said: 正如@ k-mera所说:

Database file will be created only if you did some operations in Database like "insert" . 只有在数据库中执行某些操作(如“插入”)时,才会创建数据库文件。

Although you say the data is cleared, I suspect that Android thinks it has not. 虽然你说数据被清除了,但我怀疑Android认为它没有。 To completely remove the data, I would remove the application and re-install it. 要完全删除数据,我会删除该应用程序并重新安装它。

Since your onUpgrade calls onCreate you could also increase the DATABASE_VERSION value which will cause the data to be dropped and re-created. 由于onUpgrade调用onCreate您还可以增加DATABASE_VERSION值,这将导致数据被删除并重新创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM