execSQL() with UPDATE doesn't update

后端 未结 2 1260
Happy的楠姐
Happy的楠姐 2020-12-21 08:30

I am trying to use rawQuery and execSQL methods for manipulating my database, instead of the .update, .insert, etc. I am

相关标签:
2条回答
  • 2020-12-21 09:08

    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.

    0 讨论(0)
  • 2020-12-21 09:16

    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();
            }
        }
    
    0 讨论(0)
提交回复
热议问题