2012-05-16 71 views
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); 
} 

回答

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]; 

希望,這幫助你.. :)

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]; 
} 
+0

試試這個代碼..希望這將解決您的問題.. –

+0

感謝名單科莫爾,它的工作,但不是在 - (空)的tableView:(UITableView的* )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath。它在按鈕上的工作,我採取了行,但問題時刪除最後一排,當我選擇第一行。 – garry

+0

如果此代碼工作,然後upvote我的答案。我認爲你沒有獲取你當前的指數值。您需要獲取當前的索引,然後在上面的代碼中傳遞該索引。 –

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]; 
} 
相關問題