Get updated rows count from SQLite in Android using a raw query?

前端 未结 4 815
不思量自难忘°
不思量自难忘° 2020-12-07 01:14

How can I get the number of rows updated using an update statement in SQLite in Android?

Note that I need to use some type of raw execute of a SQL state

4条回答
  •  一向
    一向 (楼主)
    2020-12-07 01:55

    To expand on Mat's answer, here's the example code for getting the updated row count:

    Cursor cursor = null;
    try
    {
        cursor = db.rawQuery("SELECT changes() AS affected_row_count", null);
        if(cursor != null && cursor.getCount() > 0 && cursor.moveToFirst())
        {
            final long affectedRowCount = cursor.getLong(cursor.getColumnIndex("affected_row_count"));
            Log.d("LOG", "affectedRowCount = " + affectedRowCount);
        }
        else
        {
            // Some error occurred?
        }
    }
    catch(SQLException e)
    {
        // Handle exception here.
    }
    finally
    {
        if(cursor != null)
        {
            cursor.close();
        }
    }
    

提交回复
热议问题