0

I have loaded some data in my tableview using SQLite database in the format 1) some value, 2) some value so on. 1 and 2 are primary key, this is my code for delete which I have put on the swipe on delete function of the table view

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self openDB];
    NSString *idofTable = [NSString new];
    NSScanner *scanner = [[NSScanner alloc] initWithString:[self.tableLogView cellForRowAtIndexPath:indexPath].textLabel.text];
    [scanner scanUpToString:@")" intoString:&idofTable];

    NSLog(@"id=%@, text=%@", idofTable, [self.tableLogView cellForRowAtIndexPath:indexPath].textLabel.text);

    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        char *error;

        NSString *sql = [NSString stringWithFormat:@"DELETE FROM History WHERE 'id' = '%@'", idofTable];

        NSLog(@"%@",sql);

        if(sqlite3_exec(db, [sql UTF8String], NULL, NULL,&error)!=SQLITE_OK)
        {
            sqlite3_close(db);
            NSAssert(0,@"Could not create table");

        }
        else
        {
            NSLog(@"Data Deleted");
            sqlite3_exec(db, "COMMIT", NULL, NULL, &error);
        }
    }
    [tableLogView reloadData];
}

The log "Data Deleted" gets printed but the data doesnt get deleted in database nor in tableview. Any help appreciated. I dont know where the error is occuring, the query seems to be correct.

5
  • Please post the Log of the sql statemnt here. Is the ID correct? Commented Aug 14, 2013 at 7:31
  • how can you say the data not deleted, by reloading tableview? are you sure the values for tableview got updated? Commented Aug 14, 2013 at 7:32
  • you should also delete the record from your table data array to refresh the tableview. Commented Aug 14, 2013 at 7:34
  • @gamerlegend This comment is not related to your question. I would suggest you to used FMDB Framework for all your database operations. You may refer this answer stackoverflow.com/a/17268032/1017893 Commented Aug 14, 2013 at 7:44
  • 1
    Thanks to code of kapil I figured out that my delete statement worked when I remove the quotes 'id' as id. I did the insert and all using quotes now bit confused in using quotes. Commented Aug 14, 2013 at 8:48

3 Answers 3

1

Try This

 if(editingStyle==UITableViewCellEditingStyleDelete)
        {
            NSString *dbFilePath =[DBclass getDBPath];

            sqlite3_stmt *deleteStmt = nil;
         if(sqlite3_open([dbFilePath UTF8String], &database)==SQLITE_OK)
        {

        NSString *deleteString=[NSString stringWithFormat:@"delete from table where ID =%d",idValue];
        const char *sql =[deleteString UTF8String];

        if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating delete view statement. '%s'", sqlite3_errmsg(database));

        if(SQLITE_DONE != sqlite3_step(deleteStmt))
            NSAssert1(0, @"Error while deleting data. '%s'", sqlite3_errmsg(database));
        else
            NSLog(@"Success in deleting the medicine.");

        sqlite3_close(database);
    }
        }
        [tableLogView reloadData];
Sign up to request clarification or add additional context in comments.

5 Comments

Na the code is not right, he doesn't even finalize nor closing the database ;)
Thanks, actually my code now works too when I removed the quotes from the 'id', thanks to your code that helped me figure out the error. The value from database gets deleted now, but it doesnt update the table view even when I call the reloadData. Any ideas??
yes you do one more thing after delete from data base you delete value from array index before reload data like that [yourArray removeObjectAtIndex:indexPath.row]; [table reloadData];
Tried it but throwing error, anyways I solved it by repopulating my database in the tablerow return event. Now I am getting error when deleting all data, my sql stmt is DELETE * FROM History. It throws Assertion failure error. Any ideas??
Bro don't use * when you fire delete statement try this DELETE FROM History
1

You arent finalising nor closing the Database.

sqlite3_finalize(stmt);

sqlite3_close(DB);

Comments

0

This is the right way to delete a row:

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

   NSString *sql = [NSString stringWithFormat:@"DELETE FROM History WHERE 'id' = '%@'", idofTable];

   const char *del_stmt = [sql UTF8String];
   sqlite3_prepare_v2(contactDB, del_stmt, -1, & deleteStmt, NULL);
   if (sqlite3_step(deleteStmt) == SQLITE_DONE)
   {
       NSLog(@"Data Deleted");
       sqlite3_exec(db, "COMMIT", NULL, NULL, &error);
   }else {
       NSLog( @"Error: %s", sqlite3_errmsg(database) );
   }
   sqlite3_finalize(deleteStmt);
   sqlite3_close(contactDB);


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.