SQLiteOpenHelper “onCreate” is not called? (the DB does not exist)

∥☆過路亽.° 提交于 2019-12-01 19:42:20

问题


From a fragment I instantiate this way

fmdata = new FileManagerData(getActivity());

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) {

    }   
}

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


回答1:


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




回答2:


Probably the database exists from an earlier try.

Uninstall your app and try again.




回答3:


You should put in onCreate this:

sqLiteDatabase.execSQL("create table" + DB_NAME + " ( _id integer primary key autoincrement, " + fieldOne + "INTEGER, " + fieldTwo + "INTEGER);");

Where fieldOne and fieldTwo are names of your table's columns. Also look here: http://www.sqlite.org/datatype3.html




回答4:


I know that this thread has already accepted answer. Neverthless this is what i have to add:

i have:

    protected class OpenHelper extends SQLiteOpenHelper {

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
             MHSqlDb.this.createTables(db);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I'm calling this:

    iOpenHelper = new OpenHelper(iDbFileName, DATABASE_VERSION );

The databese file doesn't exist at this stage.

Then i do some debugging of the app. i never call

    iOpenHelper.getReadableDatabase();

neither i call

    iOpenHelper.getWritableDatabase();

the database file still does not exist. Now when i kill the app from debugger i can suddenly see database file (with the name in iDbFileName) created. The onCreate method of the OpenHelper is never called.

So while i agree that the intention of onCreate method seems to be to get called when the database file doesn't exist and either getReadableDatabase() or getWritableDatabase() is called, this is apparently not always the case.




回答5:


Uninstalling the app will remove the db, and thus will call onCreate on next run.



来源:https://stackoverflow.com/questions/10147888/sqliteopenhelper-oncreate-is-not-called-the-db-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!