1

how can rename column of table in SQlite database android when upgrade database_version

  @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        if (DATABASE_VERSION == 2) {
           ????
        }

        onCreate(db);
    }

forpas
  • 160,666
  • 10
  • 38
  • 76
mahsa k
  • 555
  • 6
  • 9
  • Possible duplicate of [Android Room: How to Migrate Column Renaming?](https://stackoverflow.com/questions/52657760/android-room-how-to-migrate-column-renaming) – Nicola Gallazzi Sep 17 '19 at 12:50
  • @NicolaGallazzi it is obvious that the OP is not using ROOM. – forpas Sep 17 '19 at 13:00
  • 1
    @forpas, sure thing, my bad! However Room is not the point here, as the SQLite syntax would be the same – Nicola Gallazzi Sep 17 '19 at 13:35
  • i dont want migrate and rename table or alter or add column .forexample my column name in last version was a and in new version will be b. i want rename column in onUpgrade method without clear table or clear last version of application – mahsa k Sep 18 '19 at 18:21

1 Answers1

2

With ALTER TABLE you can change the column's name (version of SQLite: 3.25.0+):

if (DATABASE_VERSION == 2) {
    db.execSQL("alter table tablename rename columnname to newcolumnname");
}
forpas
  • 160,666
  • 10
  • 38
  • 76