简体   繁体   English

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

[英]how can i delete it data row from table view in 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);
}
 - (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];
}

what you use here for connect with sqlite?? 您在此处用于与sqlite连接的内容?? ie, if you use coredata then its code so easy with small logic what you use??? 即,如果您使用coredata,那么使用小逻辑就很容易使用它的代码?

like... 喜欢...

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

}

after that reload table with reloadData method 之后使用reloadData方法重新加载表

[yourTable reloadData];

hope,this help you.. :) 希望,这对您有帮助.. :)

Perhaps, this will help you. 也许,这对您有帮助。 Where _allDownloadedSongs is the array from which you are filling table, and deleteRowFromDatabase is a method to delete the specific row from array, database and talbeView. 其中_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];
    }
}

//And For Database Deleting //并用于数据库删除

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM