我想从我的数据库表 User 中只删除一行。
我尝试了this,但我不知道如何使用SQLITE_ENABLE_UPDATE_DELETE_LIMIT
。
这是我的代码:
SQLiteDatabase sqLiteDatabase = this.openOrCreateDatabase("User", MODE_PRIVATE, null);
//sqLiteDatabase.execSQL("INSERT INTO user (name, age) VALUES ('jihyo', 10)");
sqLiteDatabase.execSQL("DELETE FROM user WHERE name ='jihyo' LIMIT 1");
Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM user WHERE name = 'jihyo'", null);
int nameIndex = c.getColumnIndex("name");
int ageIndex = c.getColumnIndex("age");
c.moveToFirst();
do {
Log.v("name", c.getString(nameIndex));
Log.v("age", c.getString(ageIndex));
} while (c.moveToNext());
这是错误:
android.database.sqlite.SQLiteException:near“LIMIT”:语法错误 (代码1):,编译时:DELETE FROM user WHERE name ='jihyo' 限制1
答案 0 :(得分:1)
您无法在删除查询中应用限制,因为删除 条款没有 LIMIT 所以你可以使用 SELECT 查询选择单个记录并在基础上应用 DELETE 所选查询 例如。
DELETE FROM user WHERE id IN
(SELECT id FROM user
WHERE name = 'jihyo'
LIMIT 1);
注意:您可以使用'='代替'IN'
答案 1 :(得分:0)
试试这个:
db.execSQL("Delete from user where user_id IN (Select user_id from user where name = 'jihyo' limit 1)");
[已更新]:=
无法使用LIMIT
操作,因此您需要按照以下方式进行操作:
Cursor c = db.execSQL("Select user_id from user where name = 'jihyo' limit 1");
if (c.moveToFirst()){
String id = c.getString(0);
db.execSQL("Delete from user where user_id = "+id);
}