使用SQLite数据库存储数据(4)删除数据记录
刪除數(shù)據(jù)記錄
當(dāng)從UITableView中刪除一行記錄時(shí),將調(diào)用commitEditingStyle方法。- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Get the object to delete from the array
NotebookInfo *notebookInfo = [noteArray objectAtIndex:indexPath.row];
[self removeNotebook: notebookInfo];
// Delete the object from the table
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)removeNotebook:(NotebookInfo *)notebookInfo {
// Delete it from the database
sqlite3_stmt *statement;
const char *sql = "delete from Notebook where id = ?";
// 編譯SQL 語句,創(chuàng)建Statement 對(duì)象
if(sqlite3_prepare_v2(noteDB, sql, -1, &statement, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(noteDB));
//When binding parameters, index starts from 1 and not zero.
sqlite3_bind_int(statement, 1, notebookInfo.pk_id);
// SQLITE_DONE 表示成功執(zhí)行了Statement
if (sqlite3_step(statement) != SQLITE_DONE)
NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(noteDB));
// Reset a prepared Statement object
sqlite3_reset(statement);
// Destroy the Statement object
sqlite3_finalize(statement);
// Remove it from the array
[noteArray removeObject:notebookInfo];
}
在上面的代碼中,我們實(shí)現(xiàn)了從3個(gè)地方刪除對(duì)應(yīng)的數(shù)據(jù)記錄:
(1) 從SQLite數(shù)據(jù)表中刪除對(duì)應(yīng)的記錄;
(2) 從noteArray 數(shù)組中刪除對(duì)應(yīng)的notebookInfo對(duì)象元素;
(3) 從UITableView 中刪除對(duì)一個(gè)的數(shù)據(jù)行;
現(xiàn)在運(yùn)行App測(cè)試一下,如果代碼運(yùn)行正常,運(yùn)行效果如下圖所示:
一起學(xué)習(xí)GIS及其二次開發(fā),一起進(jìn)步!
總結(jié)
以上是生活随笔為你收集整理的使用SQLite数据库存储数据(4)删除数据记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5 结构型模式之 - 适配器模式
- 下一篇: 信用卡可以互相还款吗?信用卡互相还款违法