user2746120
user2746120

Reputation:

Increment in Android SQLite

I want to update the col_Item_Auto_ID column with +1 in each column

Like this: replace 10 with 11, 11 with 12, etc!! Help me pls... here the code is,

 public void updateMiddleTemp(int autoid) {

        SQLiteDatabase db = this.getWritableDatabase();
        int id=51;


        ContentValues cv_temp1 = new ContentValues();
        cv_temp1.put(col_Item_Auto_ID, col_Item_Auto_ID+1);
        //ff

        long stemp = db.updateWithOnConflict(Table_KOT_ITEMS_TEMP, cv_temp1,
                col_Item_Auto_ID + " >= " + autoid, null,SQLiteDatabase.CONFLICT_IGNORE);
    db.close();
    }

Upvotes: 6

Views: 3655

Answers (4)

Nidheesh
Nidheesh

Reputation: 443

This code worked for me:

String query = "UPDATE " + Constants.RecentChats.DB_RECENT_CHATS + " SET "
                         + Constants.RecentChats.UNREAD_COUNT + " = "
                         + Constants.RecentChats.UNREAD_COUNT +"+1"+ " WHERE "
                         + Constants.RecentChats.JID + " =" + id;

db.execSQL(query);

Upvotes: 0

ruwan800
ruwan800

Reputation: 1887

This can be done with updateWithOnConflict().

The value provided for the value field in ContentValues is incorrect. Resulting value should be "col_Item_Auto_ID + 1" and not "col_Item_Auto_ID1". So try changing

cv_temp1.put(col_Item_Auto_ID, col_Item_Auto_ID+1);

to

cv_temp1.put(col_Item_Auto_ID, col_Item_Auto_ID+"+1");

Upvotes: 0

Exceptional
Exceptional

Reputation: 3004

Try this code, it works fine:

 db.execSQL("UPDATE " + Table_KOT_ITEMS_TEMP + " SET "
                    + col_Sl_No + " = " + col_Sl_No + " +1 WHERE "
                    + col_Sl_No + " >" +into);
db.close();

Upvotes: 12

Aleks G
Aleks G

Reputation: 57316

You cannot do this with standard update method - you need to use execSql method instead:

String sql = "UPDATE " + Table_KOT_ITEMS_TEMP +
             " SET " + col_Item_Auto_ID + "=" + col_ITEM_AUTO_ID + "+1" +
             " WHERE " + col_ItemAutoID + " >= " + autoid;

db.execSql(sql);

Upvotes: 7

Related Questions