致命 - 运行时异常 SQLite 从数据库中删除数据



>我正在尝试从源列等于"在线"的SQLite表中删除数据

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reminders/com.reminders.FetchDataActivity}: android.database.sqlite.SQLiteException: no such column: Online (code 1): , while compiling: delete from ReminderTable where source = Online

这是我的代码:

public void deleteOnlineReminders() {
        SQLiteDatabase db = this.getReadableDatabase();
        db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + " = " + "Online");
    }

Or 是否由于我没有任何数据进入列源 = 在线的表格

试试这段代码。

db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + "=?",new String[] { "Online" });

你必须在字符串连接中使用"Online"(注意引号)。

从异常中可以看出,编译语句在其他方面是错误的,因为您引用的是列,而不是字符串。

您可能还希望对参数使用预准备语句而不是字符串连接:如何在 Android 的 SQlite 中使用预准备语句?

这是更干净和安全的版本:)

public void deleteOnlineReminders() {
    SQLiteDatabase db = this.getWritableDatabase();       
    dbHelper.delete(TABLE_REMINDERS, KEY_SOURCE + "=?", new String[] { Online})
}

最新更新