Android:删除数据库中的特定行

发布于 2024-10-22 07:57:58 字数 514 浏览 1 评论 0原文

抱歉,如果这看起来很明显。我正在尝试编写一个方法来从字符串 showId 中删除一行。最好的方法是什么?游标只能用于选择还是也可以用于删除和更新? 这是我目前采用的两种方法:

public int deleteShowById1(String showId){
    Cursor cursor = db.rawQuery("DELETE FROM tblShows WHERE showId = '" + showId+"'", null);
    if (cursor.moveToFirst()) {
        return 1;
    } else
        return -1;
}

    public int deleteShowById2(String showId) {
    String table_name = "tblShows";
    String where = "showId='"+showId+"'";
    return db.delete(table_name, where, null);
}

Sorry if this seems obvious. I'm trying to write a method to delete a row from a String showId. What would be the best way, and can Cursors only be used for Selects or also for Deletes and Updates?
These are the two methods I'm at so far:

public int deleteShowById1(String showId){
    Cursor cursor = db.rawQuery("DELETE FROM tblShows WHERE showId = '" + showId+"'", null);
    if (cursor.moveToFirst()) {
        return 1;
    } else
        return -1;
}

    public int deleteShowById2(String showId) {
    String table_name = "tblShows";
    String where = "showId='"+showId+"'";
    return db.delete(table_name, where, null);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱情眠于流年 2024-10-29 07:57:58

从mysql查询我们知道,在android中也是一样的。

    String query = "DELETE FROM " +TABLE_NAME+ " WHERE "  + COLUM_NAME+ " = " + "'"+VALUE +"'" ;

    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(query);
    db.close();

VALUE 可能有也可能没有单引号,具体取决于数据类型。

As we know from mysql query, it is same here in android.

    String query = "DELETE FROM " +TABLE_NAME+ " WHERE "  + COLUM_NAME+ " = " + "'"+VALUE +"'" ;

    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(query);
    db.close();

VALUE may or may not have single quotation depending on datatype.

稀香 2024-10-29 07:57:58

我倾向于使用第二种方法 (db.delete),因为我认为使用 rawQuery 是不受欢迎的。

如果您进行选择,然后循环光标进行更新或删除,这是有意义的,但是传递光标进行删除或更新对我来说没有意义,因为程序不知道如何解析游标结果以获得正确的字段。

I tend to use the second method (db.delete), as I think using rawQuery is frowned upon.

If you do a select, then loop through the cursor to do updates or deletes, that would make sense, but to pass a cursor to do the delete or update doesn't make sense to me, as the program won't know how to parse the cursor results to get the correct fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文