如何update在Android中的SQLite中使用语句更新行数?
请注意,我需要使用某种类型的SQL语句的原始执行,而不是做任何事情ContentValues因为我需要动态查询.因此我无法使用该SQLiteDatabase.update()方法.例如,我正在运行类似的东西
UPDATE my_table
SET field_name = field_name + 1
Run Code Online (Sandbox Code Playgroud)
我知道的方法返回void,例如SQLiteDatabase.execSQL(). SQLiteDatabase.rawQuery()返回a Cursor,但是游标的行数为零,其计数始终为-1.
Pan*_*ang 11
要扩展Mat的答案,这是获取更新行数的示例代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
扩展Mat和Pang的答案...
我们可以跳过游标并使用它simpleQueryForLong()吗?
例如
public long getChangesCount() {
SQLiteDatabase db = getReadableDatabase();
SQLiteStatement statement = db.compileStatement("SELECT changes()");
return statement.simpleQueryForLong();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10060 次 |
| 最近记录: |