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

前端 未结 10 719
死守一世寂寞
死守一世寂寞 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条回答
  • 2020-12-08 18:44

    Call getWritableDatabase(); in the constructor

    public DataBaseH(@Nullable Context context) {
        super(context, dataBaseName, null, dataBaseVersion);
        SQLiteDatabase db=this.getWritableDatabase();
    
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
     String createTable="CREATE TABLE IF NOT EXISTS "+tableName+                              " ( "+
                id+  " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"+
                name+                              " TEXT,"+
                familyName+                        " TEXT,"+
                age+                           " INTEGER);";
    
        db.execSQL(createTable);
    
         Log.i(TAG,"db.exect");
    
    
    }
    
    0 讨论(0)
  • 2020-12-08 18:46

    Until you call the method getWritableDatabase() or getReadableDatabase() of SQLiteOpenHelper class, database won't be created.

    as simple as that database will be created in memory when you actually need that.:)

    0 讨论(0)
  • 2020-12-08 18:46

    I was having a similar problem with onCreate() not executing when the app was very first run, so my database never got created. This is NOT the same as when onCreate() is not executing because the database already existed, because the database did not yet exist. Specifically, my DataProvider onCreate() was not executing, so the OpenHelper never got called either.

    I verified I had everything set up the way that everyone described in the previous answers, but nothing resolved my problem. Posting this answer in case anyone else forgets one small detail like I did.

    What resolved the problem for me was adding a entry in AndroidManifest.xml for my Data Provider, nested inside the tags, along with all of my entries. The only attributes I needed were:

    • android:name=".DataManagement.DbDataProvider"
    • android:authorities="com.example.myApplicationName.DataManagement.DbDataProvider"
    • android:exported="false"

    (Make sure to change the values for the above attributes to match your project)

    I cleaned, built, ran, and onCreate() methods for the data provider and open helper classes executed properly, and the database was created on first application launch!

    0 讨论(0)
  • 2020-12-08 18:48

    I had the same problem.. the resolution for me was to add .db as extension of the database name

    0 讨论(0)
  • 2020-12-08 18:50

    You can change AUTOINCREMENT to AUTO INCREMENT

    Note SQLiteOpenHelper Called onCreate when the database is created for the first time. If you create table in onCreate method you can't create new SQLiteDatabase. You can see example

        @Override
        public void onCreate(SQLiteDatabase db) {
            String stringCreateTable = "CREATE TABLE "+"tblUser"+" ( " +
                    "id TEXT PRIMARY KEY, " +
                    "name TEXT )";
            db.execSQL(stringCreateTable);
        }

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题