SQLiteOpenHelper"的onCreate"不叫? (该数据块不存在)不存在、不叫、数据、SQLiteOpenHelper

2023-09-07 13:17:51 作者:此人危險、請勿靠近

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

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.