SQLiteOpenHelper"的onCreate"不叫? (该数据块不存在) [英] SQLiteOpenHelper "onCreate" is not called? (the DB does not exist)

查看:110
本文介绍了SQLiteOpenHelper"的onCreate"不叫? (该数据块不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个片段,我实例这样

From a fragment I instantiate this way

fmdata = new FileManagerData(getActivity());

下面的类。我不明白为什么的onCreate()将不会调用和我的数据库还没有生成。

the following class. I don't understand why onCreate() is not called and my database does not get created.

public class FileManagerData {
public static final String TAG = FileManagerData.class.getSimpleName();;


Context context;
DBHelper dbHelper;

public FileManagerData (Context context){
    this.context = context;
    dbHelper = new DBHelper();
}

private class DBHelper extends SQLiteOpenHelper{

    private static final String DB_NAME = "filename.db";
    private static final String DB_SQL = "filename.sql";
    private static final int DB_VERSION = 1; // internal number

    public DBHelper() {
        super(context, DB_NAME, null, DB_VERSION);  
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            // this is NEVER called and my DB does not exist yet
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }   
}

修改:诀窍是数据库必须使用(读取或写入),这样的onCreate()被调用

EDIT: The trick is that the database has to be used (to read or write) so onCreate() get called.

推荐答案

的onCreate 方法会后的第一次访问数据库调用。做一个查询到DB和的onCreate 将被调用。

The onCreate method will be called after first access to the DB. Make a query to the DB and onCreate will be invoked.

这篇关于SQLiteOpenHelper"的onCreate"不叫? (该数据块不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆