【问题标题】:Fatal - RuntimeException SQLite Remove Data from Database致命 - RuntimeException SQLite 从数据库中删除数据
【发布时间】:2016-04-25 09:47:15
【问题描述】:

我正在尝试从源列等于“在线”的 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 是不是因为我没有任何数据到列源=在线的表中

【问题讨论】:

  • 在执行此操作之前,只需检查表是否已创建,异常似乎没有名为 KEY_SOURCE 的列

标签: android sqlite where-clause runtimeexception


【解决方案1】:

试试这个代码。

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

【讨论】:

  • 现在得到:java.lang.RuntimeException:无法启动活动 ComponentInfo{com.reminders/com.reminders.FetchDataActivity}:android.database.sqlite.SQLiteException:接近“=”:语法错误(代码 1): ,编译时:从 ReminderTable 中删除 source =
  • @Sun 我刚刚编辑只添加了Question Mark After Equal to 试试看。
  • 谢谢...为什么我们在等于之后使用问号
  • @Sun 用于传递您在 where 条件下给出的字段值。就是这样。
【解决方案2】:

您必须在字符串 concat 中使用 'Online'(注意引号)。

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

您可能还想为参数使用准备好的语句而不是字符串连接: How do I use prepared statements in SQlite in Android?

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

【讨论】:

  • 我也试过这种方式:db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + " = Online");但仍然遇到同样的异常
  • db.execSQL("delete from "+ TABLE_REMINDERS + " where " + KEY_SOURCE + " = 'Online'" 注意 Online 周围的附加单引号
【解决方案3】:
public void deleteOnlineReminders() {
    SQLiteDatabase db = this.getWritableDatabase();       
    dbHelper.delete(TABLE_REMINDERS, KEY_SOURCE + "=?", new String[] { Online})
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2011-08-24
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多