Skip to content Skip to sidebar Skip to footer

Sqlite In Android Not Recognizing New Column I Added

My db in my app worked fine, then I added odometer to the db. Now I am getting a table inspections has no column named odometer: , while compiling: INSERT INTO inspections(codrive

Solution 1:

SQLiteOpenHelper is seeing your existing database and so it isn't calling onCreate.

You need to uninstall your app so that you get rid of the existing database.

If you're in dev mode, you can just change the DATABASE_NAME to db2 as a quick hack to get yourself going, but remember to change it back later once your database is stable.

If you want to do it in the most proper way, you can increment DATABASE_VERSION, then implement some ALTER TABLE statements in onUpgrade.

Solution 2:

First time when you open the database then the onCreate method of the SQLiteOpenHelper will call and then after it will not call.

And onUpgrade method will be call when you upgrade the database version. So suppose you create your database using the Sqlite version 1.0 then for update the database you will have to change the database version greater than the current version of the database.In your case you will have to change the DATABASE_VERSION variable value to 2.

Note:- If you change the database version then it will call the onUpgrade method so it will execute all the code in this method so you will have to put only those queries for the tables which you want to update else if you put all the tables update queries then it will update all the tables of your database so may be you lost all your previous data from the old database.

Solution 3:

I think the simplest way to handle this is as follows:

  1. Make any changes you want to your table(s)

  2. Increment your database version by 1

  3. Drop the table(s) completely in onUpgrade()

  4. Recreate the table(s)/the entire Database within onUpgrade() by calling onCreate() method.

  5. Run your app and the changes to the database or table(s) should be reflected successfully.

Here is the code sample:

@OverridepublicvoidonCreate(SQLiteDatabase db) {
            db.execSQL(CurrentStateTable_TABLE_CREATE);
            Log.i(LOGTAG, "CurrentStateTable Table created");

            db.execSQL(HoursOfServiceTable_TABLE_CREATE);
            Log.i(LOGTAG, "HoursOfServiceTable Table created");

            db.execSQL(InspectionsTable_TABLE_CREATE);
            Log.i(LOGTAG, "InspectionsTable Table created");

        }

        @OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + CurrentStateTable);
            db.execSQL("DROP TABLE IF EXISTS " + HoursOfServiceTable);
            db.execSQL("DROP TABLE IF EXISTS " + InspectionsTable);
            onCreate(db);

            Log.i(LOGTAG, "Database successfully upgraded " + "from " + oldVersion + " to " + newVersion);
        }

Post a Comment for "Sqlite In Android Not Recognizing New Column I Added"