0

I want to know .

I have one Table

create table User (userid INTEGER PRIMARY KEY AUTOINCREMENT, username text,address text)

and i am creating this table on onCreate()

and inserting the value.

INSERT into User (username,address) VALUES('noname','xyz')

output-username--noname address--xyz

and on onUpgrade()

i added one column in above table

 ALTER TABLE User ADD COLUMN City text

and insert value INSERT into User(City) VALUES('delhi')

when i launch the app the output its showing.

output- username--null address--null city--delhi

why its showing null for both column .even i did not drop the table

it should show something like this

output- username--noname address--xyz city--delhi

please clarify this behavior of onUpgrade

like this:-

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

        {
            db.execSQL("ALTER TABLE User ADD COLUMN City TEXT");
            db.execSQL("INSERT into User (City) VALUES(delhi)");
        }
Community
  • 1
  • 1
Monty
  • 3,205
  • 8
  • 36
  • 61

1 Answers1

3

That is what the INSERT command does, it creates entirely new records.

To update the values in existing records, use the UPDATE command:

UPDATE User SET City = 'Delhi' WHERE username = 'noname'
CL.
  • 173,858
  • 17
  • 217
  • 259