user3684678
user3684678

Reputation: 561

Sqllite database updated rows become last row of database

My database table consists of many rows where unique key is the id for which I'm using unique identifier. So no duplicate items are there.

I am using replace() method to add items to database so that if any column is updated then it is replaced by new value. But the issue is that suppose my first row of database is updated then it will become the last row with the updated value.Also i don't know which column of row will be updated as the data is coming from server..

If i use insert() method in place of replace() for adding items then items will not be updated.What should I do so that my rows are also replaced and the order of items should not be changed after update.

Code-

 String CREATE_VIDEO_TABLE = "create table "
                + TABLE_VIDEO + "(" + KEY_ID + " text not null unique, "
                + KEY_CAPTION + " text not null, "
                + KEY_LOCATION + " text not null, "
                + KEY_NOSHARES + 
                 " text not null);";


      public void addVideoData(Video videoSql) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_ID, videoSql.getId());
            values.put(KEY_CAPTION, videoSql.getCaption());
            values.put(KEY_LOCATION, videoSql.getLocation());
            values.put(KEY_NOSHARES, videoSql.getNoOfShares());

            try {
                db.replace(TABLE_VIDEO, null, values);
            }catch(Exception e){}

            db.close();
        }

Upvotes: 0

Views: 66

Answers (2)

Apurva
Apurva

Reputation: 7901

If you have created your updateRow method right way then it won't add an updated row at the last but it will replace it rather and won't change the id either.

To update Db your method should be like,

public void updateRow(int id ,String name, String address, String date){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(COLUMN_ID, id);
    values.put(COLUMN_NAME, name);
    values.put(COLUMN_ADDRESS, address);
    values.put(COLUMN_DATE, date);

    db.update(TABLE_NAME, values, COLUMN_ID+ " = " +id, null);
    db.close();
}

Edit:

You don't need to use replace() method in your case, use update() method. This will not put updated row at the last, and will also keep the id same.

Change

try {
            db.replace(TABLE_VIDEO, null, values);
        }catch(Exception e){}

To

db.update(TABLE_VIDEO, values, KEY_ID+ " = " +videoSql.getId(), null);

If you use this same method (the method you have posted in question) to insert data and update data it will always put your row at last, you need to create one another method to insert data in database. It should be as follow.

public void insertData(Video videoSql){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_CAPTION, videoSql.getCaption());
    values.put(KEY_LOCATION, videoSql.getLocation());
    values.put(KEY_NOSHARES, videoSql.getNoOfShares());
    .
    .
    .
    .
    .

    db.insert(TABLE_VIDEO, null, values);
    db.close();
}

Upvotes: 2

cheko506
cheko506

Reputation: 368

You need to use update(table_name, contentValues, where clause, bind parameters), pass the id as where clause, and must work

Upvotes: 0

Related Questions