Skip to content Skip to sidebar Skip to footer

Android Row Not Updating In Sqlite Db

I have an app where i am trying to update a row in the Transactions table in my DB. Here's the table. //table transactions column names public static final String C_ID = BaseCo

Solution 1:

You must use INTEGER PRIMARY KEY to get an autoincrementing column; INT is not enough.

Solution 2:

Your code that reads the ID from the cursor looks correct, assuming that the table actually contains that column.

Anyway, reading all fields of all rows of the table just to get one value is inefficient, and without an ORDER BY, there is no guarantee that the latest record is the last one.

I'd recommend this query to read the last ID:

cursor= db.rawQuery("SELECT max(" + LoginValidate.C_ID + ")" +
                     " FROM " + DBHelper.TABLETRANSACTIONS, null);

However, you can plug this as subquery directly into the update query:

int res = db.update(DBHelper.TABLETRANSACTIONS, values,
                    LoginValidate.C_ID + " = " +
                     "(SELECT max("+LoginValidate.C_ID+")" +
                     " FROM " + DBHelper.TABLETRANSACTIONS + ")", null);

Solution 3:

String [] str newString[c.getCount()];
int i = 0;
            while (c.moveToNext()) {
                str[i] = c.getString(c.getColumnIndex(LoginValidate.C_ID));
                i++;
            }

Post a Comment for "Android Row Not Updating In Sqlite Db"