I have created an sqlite table using the following statement:
"CREATE TABLE IF NOT EXISTS reminders (ID INTEGER PRIMARY KEY,Name VARCHAR(64), Event VARCHAR(64), Date VARCHAR(64), Bfr VARCHAR(64), Val VARCHAR(64),Num VARCHAR(64), Bod VARCHAR(400) ,Grp VARCHAR(64))";
I have successfully inserted data and retrieved data from sqlite database and displayed the reminders in view reminders too programmatically!
But I am struck with deletion of records from sqlite database.I mean I have used a model class to hold the values like name,event,date etc…In add reminder page,I have assigned the values for the variables declared in the model class,i.e.
textField.text = remClass.Name etc..
My description might sound confusing,but the above information is always useful to answer this question with ease.
I have add reminder page,where user enters data and clicks save,then data gets inserted in to database.There is View Reminders page where the user can view the saved reminder.In that there are 3 different ways to view reminders:viewAll,viewMonthly,viewGroup.Now in view all I was able to display the unique ID for every reminder using the following statement:
ReminderClass *loadedReminder = [[ReminderClass alloc] init];
loadedReminder.reminderID = sqlite3_column_int(statement, 0);
Where reminder class is a model class which holds all the values entered in add reminder page.
In order to display the saved reminder,I have used an array and added the model class object after retrieving every row,i.e. id,name,event,date etc.. and populated the table view accordingly.Say if i save 3 reminders,then sections are 3 like wise and so on…
Now if I click the edit button in view group reminders page,then as we know we can delete the row(section) of table view by clicking the orientation button on left.
If I delete the row,say a row with reminder.Then the data corresponding to that reminder should also be deleted from sqlite database.
How can I do this,I have gone through this link sounds similar to my requirement.But its not working.
Finally I have implemented the following code for deletion:
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.grpArray objectAtIndex:indexPath.section];
if(sqlite3_open([databasePath UTF8String], &remindersDB)==SQLITE_OK)
{
sqlite3_stmt *compiledstatement;
int pk = sqlite3_column_int(compiledstatement, 0);
NSString *querySQL = [NSString stringWithFormat:@"delete from reminders where ID = %i",pk];
const char *sqlstmt=[querySQL UTF8String];
if(sqlite3_prepare_v2(remindersDB, sqlstmt, -1, &compiledstatement, NULL)==SQLITE_OK)
{
if(SQLITE_DONE != sqlite3_step(compiledstatement))
NSAssert1(0,@"Error while creating delete statement => %s",sqlite3_errmsg(remindersDB));
}
NSLog(@"delete DONE");
sqlite3_finalize(compiledstatement);
}
sqlite3_close(remindersDB);
[self.gTable reloadData];
}
RETRIEVAL CODE:
-(void)loadgReminders
{
EventsReminderAppDelegate *appDelegate = (EventsReminderAppDelegate *)[[UIApplication sharedApplication]delegate];
NSLog(@"String = %@",appDelegate.groupString);
self.grpArray = nil;
self.grpArray = [[NSMutableArray alloc]init];
//Retrieve the group of reminder
const char *thePath = [self.databasePath UTF8String];
sqlite3_stmt *statment;
if (sqlite3_open(thePath, &remindersDB) == SQLITE_OK)
{
NSString *getQuery = [NSString stringWithFormat:@"SELECT * FROM reminders WHERE Grp = '%@' ORDER BY Date ASC",appDelegate.groupString];
const char *sqlite_stmt = [getQuery UTF8String];
if (sqlite3_prepare_v2(self.remindersDB, sqlite_stmt, -1, &statment, NULL) == SQLITE_OK)
{
while (sqlite3_step(statment) == SQLITE_ROW)
{
ReminderClass *remind = [[ReminderClass alloc]init];
remind.reminderID = sqlite3_column_int(statment, 0);
**NSLog(@"reminderID=%d",remind.reminderID);**
remind.Name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 1)];
remind.Event = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 2)];
remind.Date = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 3)];
remind.numDays = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 4)];
remind.selString = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 5)];
remind.number = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 6)];
remind.msgBody = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 7)];
remind.remGroup = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statment, 8)];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:remind.Date];
[dateFormat setDateFormat:@"MMMM dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remind.Date = dateVal;
[self.grpArray addObject:remind];
[remind release];
}
sqlite3_finalize(statment);
}
sqlite3_close(remindersDB);
}
}
There I used NSLog for checking whether reminder ID values are getting displayed properly or not it is getting displayed as:

But what’s wrong when I try to do the same with deletion to retrieve the id values and assign that id value to where id=?.
It’s not working, where am I wrong?
Can any one please help me out?
You can do like this. This will definitely work out
pass the value of id in sqlite3_bind_int() method.