1
-(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);
}
4

3 回答 3

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];
}
于 2012-05-16T11:12:40.633 回答
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];
}
于 2012-05-16T13:01:09.767 回答
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];

希望,这对你有帮助.. :)

于 2012-05-16T11:07:35.443 回答