【问题标题】:How to delete a row from a table in SQLite android? [duplicate]如何从 SQLite android 中的表中删除一行? [复制]
【发布时间】:2012-01-03 05:27:15
【问题描述】:

我已经这样做了,但它不起作用。我收到force close

public boolean favoriteDelete(int id) {
    return database.delete("FavoriteData", "Google" + "=" + id, null) > 0;
}

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    你可以简单地使用sql查询来删除。

    public void delete(String id) {
            db.execSQL("delete from "+TBL_NAME+" where Google='"+id+"'");
        }
    

    在您的查询中,您传递 null 代替 whereArgs

    db.delete(table, whereClause, whereArgs)
    

    应该是这样的

    db.delete(TBL_NAME, "Google=?", new String[]{Integer.toString(id)});
    

    【讨论】:

      【解决方案2】:

      试试这个

      public boolean favoriteDelete(int id) {
          return db.delete(DATABASE_TABLE, KEY_ROWID +  "=" + id, null) > 0;
      }
      

      【讨论】:

      • 我能知道你的回答和用户的问题有什么不同吗?
      【解决方案3】:
      database.delete("tablename", "column_name=?", new String[] {Integer.toString(id)});
      
      • where 条件值应作为字符串数组给出

      【讨论】:

        【解决方案4】:

        使用占位符比使用字符串操作更好。对 int 来说没问题,但是一旦你把一个字符串放在那里,事情就会出错。

        String where = COLUMN_NAME_ADDRESS + " = ?";
        String[] whereArgs = { String.valueOf(address) };
        
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        db.delete(TABLE_NAME_DEVICES, where, whereArgs);
        

        【讨论】:

          【解决方案5】:

          在 where 子句中添加单引号...

          return database.delete("FavoriteData", "Goggle" + "='" + id+"'", null) > 0;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-12-24
            • 1970-01-01
            • 2022-01-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-10
            相关资源
            最近更新 更多