LoginSignup
3
2

More than 5 years have passed since last update.

AndroidのSQLiteでrawQueryを使ってDELETE

Posted at

1.AndroidでSQLite

AndroidでSQliteを使う時、DELETE文をrawQueryで実行する時。Cursorに対してrawQueryを実行した後にmoveToFirst()を実行しないと実際にはレコードの削除が実行されない。


// サンプル
SQLiteDatabase db;
db = dbHelper.getWritableDatabase();

// SQL文
String sqlStr = "DELETE FROM " + TABLE_NAME + " WHERE " + COneRowInfo.COL_ID + " = " + alarmId;

// SQL実行
Cursor cursor = db.rawQuery(sqlStr, null);
// 以下の文を実行しないとRecordは削除できない!
cursor.moveToFirst();

なぜcursor.moveToFirst()が必要なのか?それは仕様なんですかね~。
あまり深く突っ込まず、ここは必要なのだと割り切りましょう!

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2