Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execSQL() with UPDATE doesn't update

I am trying to use rawQuery and execSQL methods for manipulating my database, instead of the .update, .insert, etc. I am trying to perform an UPDATE with the following code:

db.execSQL("UPDATE configuration " +
                "SET number_fields = " + number_fields + ", "
                + "frequency = " + frequency + ", "
                + "ag = " + ag + ", "
                + "number_alarms = " + number_alarms + ", "
                + "failed_rapper = " + failed_rapper + ", "
                + "max_mv = " + max_mv + ", "
                + "max_nav = " + max_nav + " "
                + "WHERE serial_id = " + Integer.toString(id) + ";");

AFter this action there is a log that states an update has occurred and it seems to work, yet when I try to do a select statement on the table, it returns with the following error:

06-10 10:01:47.564: W/System.err(3815): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

Now I have performed that same SELECT on different data in Android that I manually inserted using SQLite Database Browser and it works fine. I have also performed that same UPADTE in the SQLite Browser and it has worked there. Therefore I know that the problem is that the UPDATE command is not working when running on Android.

Question: Why does the UPDATE command not work within the execSQL() method?

like image 458
TronicZomB Avatar asked Nov 17 '25 10:11

TronicZomB


2 Answers

From the Android SQLiteDatabase class documentation:

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to use insert(String, String, ContentValues), update(String, ContentValues, String, String[]), et al, when possible.

Then later:

For UPDATE statements, use any of the following instead.

update(String, ContentValues, String, String[])

updateWithOnConflict(String, ContentValues, String, String[], int)

As far as I can tell, the execSQL method is more for higher level database operations, such as creating tables and changing schema, and the .query, .update, .delete, etc. methods should be used to modify rows. I'm not sure you have another option besides .update to perform this operation.

like image 193
MattDavis Avatar answered Nov 19 '25 01:11

MattDavis


here is an sample update Query:

public boolean updateCartTable(String retailerName, String mCouponId){
        final ContentValues values = new ContentValues();

        values.put(TableConstantName.COUPON_ONLY_STATUS, 1);
        values.put(TableConstantName.COUPON_SRORE_DEALS_STATUS, 1);
        values.put(TableConstantName.COUPON_CATAGORY, "C");
        try {
            sDb.beginTransaction();
            final boolean state = sDb.update(TableConstantName.COUPON_TABLE, values, TableConstantName.CART_COUPON_RETAILER_NAME + "=" + "'"+retailerName+"'"+ " AND "+TableConstantName.CART_COUPON_ID + "=" + "'"+mCouponId+"'", null)>0;
            sDb.setTransactionSuccessful();
            return state;
        } catch (SQLException e) {
            throw e;
        } finally {
            sDb.endTransaction();
        }
    }
like image 39
Yajneshwar Mandal Avatar answered Nov 18 '25 23:11

Yajneshwar Mandal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!