使用SQLite数据库存储数据(4)删除数据记录
刪除數據記錄
當從UITableView中刪除一行記錄時,將調用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 語句,創建Statement 對象
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 表示成功執行了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];
}
在上面的代碼中,我們實現了從3個地方刪除對應的數據記錄:
(1) 從SQLite數據表中刪除對應的記錄;
(2) 從noteArray 數組中刪除對應的notebookInfo對象元素;
(3) 從UITableView 中刪除對一個的數據行;
現在運行App測試一下,如果代碼運行正常,運行效果如下圖所示:
一起學習GIS及其二次開發,一起進步!
總結
以上是生活随笔為你收集整理的使用SQLite数据库存储数据(4)删除数据记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5 结构型模式之 - 适配器模式
- 下一篇: [转]Unity-移动设备可用的压缩解压