SQLite Tutorial – Deleting Data
In the second part of the SQLite tutorial, I will show how to delete data from UITableView and from the database. This picks up from the first part of the SQLite tutorial, where I show how to select some data from the database and display it to the user.
- SQLite Tutorial – Displaying data in a UITableView.
- SQLite Tutorial – Deleting data from UITableView.
- SQLite Tutorial – Inserting data into SQLite database.
- SQLite Tutorial – Loading data as required.
- SQLite Tutorial – Updating data in SQLite database.
- SQLite Tutorial – Saving images in the database
Introduction
Sometimes we do need to delete data and this is what we will learn to do in this tutorial. If you haven’t read the part 1 of this tutorial you can read it here.
Deleting a row from UITableView
To delete data, we first need to delete the row from the table and then delete it from the UITableView, it is not necessary to do it in that order. Data can be deleted by using the edit button or by swiping your finger across the UITableViewCell.
When a row is deleted from the UITableView “commitEditingStyle” method is called which can be found in RootViewController.m.
This is how the source code looks like
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Get the object to delete from the array.
Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row];
[appDelegate removeCoffee:coffeeObj];
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
We first check if the editingStyle is “UITableViewCellEditingStyleDelete” or not, if it is then we first delete the row from the database and then from the UITableView using “deleteRowsAtIndexPaths” method.
Since we have not yet implemented “removeCoffee” method, let’s do that now.
Deleting rows from the database
Declare “removeCoffee” in “SQLAppDelegate.h” file which takes a “Coffee” object as a parameter and returns void, the code looks like this
//Complete code listing not shown
- (void) removeCoffee:(Coffee *)coffeeObj;The method does two things, sends a message to the Coffee Object to delete itself from the database and removes the object from the coffeeArray. The method is implemented in SQLAppDelegate.m file and looks like this
- (void) removeCoffee:(Coffee *)coffeeObj {
//Delete it from the database.
[coffeeObj deleteCoffee];
//Remove it from the array.
[coffeeArray removeObject:coffeeObj];
}
The first line, it sends a “deleteCoffee” message to the coffee object, let’s see how the code for that looks like
//Method is declared in the header file.
//Full code listing not shown
- (void) deleteCoffee;“deleteCoffee” is implemented in SQLAppDelegate.m file.
- (void) deleteCoffee {
if(deleteStmt == nil) {
const char *sql = "delete from Coffee where coffeeID = ?";
if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
}
//When binding parameters, index starts from 1 and not zero.
sqlite3_bind_int(deleteStmt, 1, coffeeID);
if (SQLITE_DONE != sqlite3_step(deleteStmt))
NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(database));
sqlite3_reset(deleteStmt);
}
The “deleteStmt” as a static variable whose type is sqlite3_stmt. Deceleration shown below
#import "Coffee.h"
static sqlite3 *database = nil;
static sqlite3_stmt *deleteStmt = nil;
@implementation Coffee
We first prepare the delete statement with the sql query “delete from Coffee where CoffeeID = ?”. The ‘?’ specifies that we have to pass a parameter to the statement, we do this using sqlite3_bind_int method since the parameter we have to pass is an int. Note that the index is specified as 1 and not 0 because when assigining parameters the index starts from 1 and not 0. sqlite_step is called to execute the delete statement and if it returns “SQLITE_DONE” it means that the row is deleted successfully. Click here to get a list of complete return codes by SQLite. We then reset the delete statement so it can be reused later, without having the need to build it.
Run your application and delete a row to test.
Conclusion
Deleting rows is very easy and straight forward. In my next tutorial, I will show you insert data in the database.
Happy Programming,
iPhone SDK Articles
Attachments
Suggested Readings
- SQLite Tutorial – Deleting data
- SQLite Tutorial – Inserting data into SQLite database
- SQLite Tutorial – Saving images in the SQLite database
- SQLite Tutorial – Displaying data in a UITableView.
- SQLite Tutorial – Deleting data from UITableView.
- SQLite Tutorial – Inserting data into SQLite database.
- SQLite Tutorial – Loading data as required.
- SQLite Tutorial – Updating data in SQLite database.
- SQLite Tutorial – Saving images in the database
8 Responses to SQLite Tutorial – Deleting Data
Tags





>Hi, I found this site very helpfull, keep posting, I really appreciate it!! Just to say that above you said “deleteCoffee” is implemented in SQLAppDelegate.m file. so I think there is where the confusion started, but moving the method to Coffee everything works out, by the way, I would like to know if there you have a tutorial about adding a navigation view into a tabview and another one to get data form an XML document and parse it to insert it into a database in sqlite, this would be really helpfull for a project in my school, well, thanks anyway!!
>@trsills The tutorial says to declare removeCoffee in the application delegate file and deleteCoffee in the cofee file. I can see how this is confusing, sorry about that.
deleteCoffee method has the delete statement variable and one instance variable called “coffeeID” change this to coffeeObj.coffeeID and it should work fine.
Happy Programming,
iPhone SDK Articles
>@Coder you need to open the database which is located at ~/Library/Application Support/iPhone Simulator/User/Applications/your app guid here.
I am curious to why you need to look inside the database, isn’t the newly added row visible in the table view?
Happy Programming,
iPhone SDK Articles
>FWIW, the text notes that the deleteCoffee declaration & method are to be in the appdelegate yet in doing so I get several erros (such as an issue w/ the deletestmt which is declared in Coffee).
I moved everything to the Coffee class though and the code works just perfect.
But I'm curious in that I went ahead and declared the method, static variable, and inserted the method within the appdelegate for kicks and rem'd everything out elsewhere.
In doing so, xcode keeps flagging'database' within the if statement where the error checking is being done in the deleteCoffee method as it thinks it's an undeclared variable!?
>Hi there,
Even when I use the terminal to access the database i don’t see any new rows in the database. So basically what I have done is…run your code on the simulator add a new coffee and price…then close the simulator…then access the database through the terminal and I only see one row which is of latte…please tell if there is something that I’m not doing which is needed to see changes in the database.
>@Coder to see the changes made in the database you will have to locate the database and open it in command line or you can use the firefox SQLLite manager.
Happy Programming,
iPhone SDK Articles
>Hi,
Can someone tell me how I can see the actual changes in the sqlite database after a new row is inserted or deleted. I can only see the ‘latte’ row if I open the SQL.sqlite file with textedit. I don’t see any changes in that file after anything new is inserted or something is deleted. Can someone please tell me where the actual database is stored or am I missing something here? Anyone please help!
>Hi,
Each of your tutorial are very good. Since I began the iphone programmation, this is the only source i’m looking at and for the moment, it’s really helpful !
Thanks !