Android SQLiteOpenHelper: Why onCreate() method is not called?

前端 未结 10 875
死守一世寂寞
死守一世寂寞 2020-12-08 18:38

I am trying to make my first Android app. I noticed that the SQLiteOpenHelper.onCreate() method is not called to create tables if the database not exists. Howev

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 18:53

    I had the same problem where it seemed that the onCreate was not executed. I thought so because my logs were not displayed. Turned out that there was something wrong with the blank spaces in my SQL_create String.

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            Log.d(LOG_TAG, "A table is created with this SQL-String: " + SQL_CREATE + " angelegt.");
            db.execSQL(SQL_CREATE);
        }
        catch (Exception ex) {
            Log.e(LOG_TAG, "Error when creating table: " + ex.getMessage());
        }
    }
    

    This is my corrected SQL-String:

    enterpublic static final String SQL_CREATE =
            "CREATE TABLE " + TABLE_VOCAB_LIST +
                    "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_GERMAN + " TEXT NOT NULL, " +
                    COLUMN_SPANISH + " INTEGER NOT NULL, "+
                    COLUMN_LEVEL + " INTEGER NOT NULL);)"; code here
    

    I had forgotten one blank space and when I added it everything worked fine.

提交回复
热议问题