Search code examples
androidandroid-sqlite

Android SQLite add new columns to table


I have a SQLite database in my app, and now I've done some changes to the app, the values etc. and need to rename (or delete and add new) column.

I've first just renamed the column name, but now i get the

sqlite.sqliteexception no such column error...

Should I use some different method for changing the table (column names) instead of this amateur straight forward approach which obviously returns this error?

UPDATE

The trick is just in changing the database version:

    public DatabaseManidzer(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    } 

   @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

            Log.w(DatabaseManidzer.class.getName(),"Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");

            db.execSQL("DROP TABLE IF EXISTS " + TABLE_VNOSI);
            onCreate(db);

        }

Solution

  • Yes, if you are using a DatabaseHelper then there is a way to upgrade the database schema.

    you can find plenty of tutorials on doing this.

    Here is one. Android update SQLite DB schema?

    The method you are looking for is onUpgrade

    And don't rename the variables which reference the actual table columns unless you change the actual column names first. You will have to use sql queries to do what you want.