如何从sqlite中的表视图中删除它的数据行

时间:2012-05-16 10:55:01

标签: iphone database sqlite

-(void) deletedata:(id) sender
{
    if(deleteStmt == nil)
    {
        const char *sql = "delete from cosmicnumber where name='%@' ";
        if(sqlite3_prepare_v2(db2, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating delete statement. '%s'", sqlite3_errmsg(db2));

        NSLog(@"%@",strNam);
    }

    //When binding parameters, index starts from 1 and not zero.
    sqlite3_bind_text(deleteStmt, 1, [strNam UTF8String], -1, SQLITE_TRANSIENT);

    if (SQLITE_DONE != sqlite3_step(deleteStmt))
        NSAssert1(0, @"Error while deleting. '%s'", sqlite3_errmsg(db2));

    sqlite3_reset(deleteStmt);
}

3 个答案:

答案 0 :(得分:1)

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
    sqlite3_stmt *stmt;

    if (sqlite3_open([[objdelegate getDBPath] UTF8String], &dbtest) == SQLITE_OK){

        NSString *deleteSQL =[NSString stringWithFormat:@"DELETE from test where test_Id=\"%@\"",[objdelegate.listOfTestId objectAtIndex:indexPath.row]];

        NSLog(@"Query : %@",deleteSQL);

        const char *delete_stmt=[deleteSQL UTF8String];

        sqlite3_prepare_v2(dbtest, delete_stmt, -1, &stmt, NULL);

        if(sqlite3_step(stmt) == SQLITE_DONE)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete" message:@"Record Deleted..!!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [alert show];
             [alert release];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete" message:@"Record Not Deleted..!!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [alert show];
            [alert release];
        }
    }

}
[self getdata];  // Here Call your main getdata function from where you are fetching your data from database
[tableView reloadData];
}

答案 1 :(得分:0)

你在这里用什么来连接sqlite? 即,如果你使用coredata然后它的代码很容易用你使用的小逻辑???

像...

-(void) deleteAllData {

    NSManagedObjectContext *moc = appDelegate.managedObjectContext;///your data which store in coredata

    //---------------Fetching and Deleting Category---------
    NSFetchRequest *fetchRequest;
    NSEntityDescription *entity;
    NSArray *arrobjects;
    NSError *error; 
    //---------------Fetching and Deleting ITems---------
    fetchRequest = [[NSFetchRequest alloc] init];
    entity = [NSEntityDescription entityForName:@"yourEntity" inManagedObjectContext:moc];  ///use your entity name..
    [fetchRequest setEntity:entity];

    arrobjects = [moc executeFetchRequest:fetchRequest error:nil];//arrobjects is array which stor youe another array or record which you want to delete

    for (NSManagedObject *managedObject in arrobjects) {

        [moc deleteObject:managedObject];
    }

    error = nil;
    [moc save:&error];

}

在使用reloadData方法

的重新加载表之后
[yourTable reloadData];
希望,这对你有所帮助.. :)

答案 2 :(得分:0)

也许,这会对你有所帮助。其中_allDownloadedSongs是从中填充表的数组,而deleteRowFromDatabase是从数组,数据库和talbeView中删除特定行的方法。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strQuery;
    NSString *deleteRow;
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
    [tblView beginUpdates];
        NSMutableDictionary *dic = (NSMutableDictionary *)[_allDownloadedSongs objectAtIndex:indexPath.row];
        deleteRow = [dic valueForKey:@"Id"];
        [self deleteRowFromDatabase:deleteRow];
        [_allDownloadedSongs removeObjectAtIndex:indexPath.row];
    [tblView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
    [tblView endUpdates];
    }
}

//和数据库删除

- (void)deleteRowFromDatabase:(NSString *)deleteRow
{
    AppDelegate *obj = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    NSString *strQuery = [NSString stringWithFormat:@"DELETE FROM DownloadedSongs WHERE Id=%d", rowId];
    [obj InsUpdateDelData:strQuery];
}