使用Sqlite

时间:2016-12-08 11:12:47

标签: ios sqlite

我正在开发IOS App,使用Sqlite来存储数据。数据可以显示在tableview上并从表视图中删除行,数据可以从数据库sqlite中删除。但是当我从tableview删除行时,应用程序崩溃了。我得到的错误是:

  

错误:原因:'无效更新:第0部分中的行数无效。   更新后现有部分中包含的行数   (9)必须等于该部分中包含的行数   在更新(9)之前,加上或减去插入的行数或   从该部分删除(0插入,1删除)和加号或减号   移入或移出该部分的行数(0移入,0   搬出去了。

先谢谢

代码:

    NSMutableArray *resultArray

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell *cell = [_tableview dequeueReusableCellWithIdentifier:@"cell"];

    dataBase *data = [_arr objectAtIndex:indexPath.row];

    UILabel *name = (UILabel *)[cell viewWithTag:1];
    name.text = data.name;
    UILabel *department = (UILabel *)[cell viewWithTag:2];
    department.text = data.department;
    UILabel *year = (UILabel *)[cell viewWithTag:3];
    year.text = data.year;

    if (indexPath.row %2 ==1)   //Use for color on tableview
    cell.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:1];
    else
    cell.backgroundColor = [UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1];
   return cell;
}
  -(void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [_tableView beginUpdates];

            Sqlite *sqlite = [[Sqlite alloc]init];
            [sqlite delete_profile:indexPath.row];

            NSMutableArray *newA = [[sqlite resultArray] mutableCopy];
            [newA removeObjectAtIndex:indexPath.row];

            [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
            [_tableView endUpdates];
        }
        [_tableView reloadData];
    }

   // Method to delete Row From Sqlite

    -(void)delete_profile:(NSInteger)value
    {
        //      _categoryArray = [[NSMutableArray alloc]init];

        NSString *databasename=@"student.db";  // Your database Name.

        NSArray * documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);

        NSString * DocDir=[documentpath objectAtIndex:0];
        NSLog(@"%@",DocDir);
        NSString * databasepath=[DocDir stringByAppendingPathComponent:databasename];


        if (sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK)
        {
            NSStringEncoding stringEncoding = NSUTF8StringEncoding;
            NSString *sql = [NSString stringWithFormat:@"DELETE FROM studentsDetail WHERE regno=\"%ld\"",(long)value];
            const char *sqlStatement = [sql cStringUsingEncoding:stringEncoding];
            if (sqlite3_prepare_v2(database,
                                   sqlStatement, -1, &statement, NULL) == SQLITE_OK)
            {
                //sqlite3_bind_int(statement, 1, value);

                if (sqlite3_step(statement) == SQLITE_OK)
                {
                    dataBase *base = [[dataBase alloc]init];
                    base.name = [[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
                    base.department = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
                    base.year = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
                    [_resultArray addObject:base];
                }
            }
            else {
                NSAssert1(0,@"Error preparing statement", sqlite3_errmsg(database));
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

不需要var drawControl = new L.Control.Draw({ draw : { position : 'topleft', polygon : { shapeOptions: { color: 'red' } }, marker: { icon: new customMarker() //Here assign your custom marker }, polyline : false, rectangle : { shapeOptions: { color: 'blue' } }, circle : false }, edit: { featureGroup: editableLayers, //REQUIRED!! remove: true } }); ;在[_tableView reloadData]之后 试试这个

deleteRowsAtIndexPaths