Android:删除数据库中的特定行
抱歉,如果这看起来很明显。我正在尝试编写一个方法来从字符串 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从mysql查询我们知道,在android中也是一样的。
VALUE 可能有也可能没有单引号,具体取决于数据类型。
As we know from mysql query, it is same here in android.
VALUE may or may not have single quotation depending on datatype.
我倾向于使用第二种方法 (
db.delete
),因为我认为使用rawQuery
是不受欢迎的。如果您进行选择,然后循环光标进行更新或删除,这是有意义的,但是传递光标进行删除或更新对我来说没有意义,因为程序不知道如何解析游标结果以获得正确的字段。
I tend to use the second method (
db.delete
), as I think usingrawQuery
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.