3

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?

4

2 回答 2

6

Android SQLiteDatabase 类文档:

执行不是 SELECT 的单个 SQL 语句或任何其他返回数据的 SQL 语句。

它无法返回任何数据(例如受影响的行数)。相反,我们鼓励您尽可能使用 insert(String, String, ContentValues), update(String, ContentValues, String, String[]) 等。

然后后来:

对于 UPDATE 语句,请改用以下任何一种。

更新(字符串,内容值,字符串,字符串 [])

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

据我所知,该execSQL方法更多用于更高级别的数据库操作,例如创建表和更改架构,并且应该使用 , , 等方法来修改.query.update。除了执行此操作之外,.delete我不确定您是否还有其他选择。.update

于 2013-06-10T15:08:58.257 回答
2

这是一个示例更新查询:

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();
        }
    }
于 2013-06-10T15:14:25.190 回答