添加的对象未使用SQlite数据库在UITableView中更新

时间:2011-08-25 14:23:11

标签: iphone uitableview sqlite

我有一个可以在UITableView上显示的SQL数据库。但是如果尝试添加任何对象,它不会在UITableView中更新。它仅在重新启动应用程序后更新。 当我从UITableView中删除对象时,内置的方法会删除 是if (editingStyle == UITableViewCellEditingStyleDelete)有没有这样的方法 用于添加对象吗?

我的添加方法如下:

static sqlite3 *addStmt = nil;   

- (void) add_Place:(Place *)PlaceObj {

    NSLog(@"Super add called");

    //Add it to the database.
    [PlaceObj addPlace];

    //Add it to the coffee array.
    [PlacesArray addObject:PlaceObj];
}

- (void) addPlace {


    NSLog(@"addplace called");
    if(addStmt == nil)
    {
        NSLog(@"inside addstmt");
        const char *sql = "INSERT INTO places_table ( PlaceName, PlaceAddress) Values (?,?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        {
            NSLog(@"error inside if loop");
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

        }
    }

    NSLog(@"outside if Loop") ;

    sqlite3_bind_text(addStmt, 1, [PlaceName UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 2, [PlaceAddress UTF8String], -1, SQLITE_TRANSIENT);

    if(SQLITE_DONE != sqlite3_step(addStmt))
    {
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));;
    }
    //  NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
    {
        //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
        PlaceID = sqlite3_last_insert_rowid(database);
        NSLog(@"sqlite3_last_insert_rowid") ;
    }

    //Reset the add statement.
    sqlite3_reset(addStmt);
}

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式执行此操作。添加对象后,只需调用ReloadData UITableView方法,它将再次重新加载表。因此无需再次重新启动应用程序。

    static sqlite3 *addStmt = nil;   

    - (void) add_Place:(Place *)PlaceObj {

        NSLog(@"Super add called");

        //Add it to the database.
        [PlaceObj addPlace];

        //Add it to the coffee array.
        [PlacesArray addObject:PlaceObj];
        [TableView ReloadData];
    }

    - (void) addPlace {


        NSLog(@"addplace called");
        if(addStmt == nil)
        {
            NSLog(@"inside addstmt");
            const char *sql = "INSERT INTO places_table ( PlaceName, PlaceAddress) Values (?,?)";
            if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            {
                NSLog(@"error inside if loop");
                NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

            }
        }

        NSLog(@"outside if Loop") ;

        sqlite3_bind_text(addStmt, 1, [PlaceName UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 2, [PlaceAddress UTF8String], -1, SQLITE_TRANSIENT);

        if(SQLITE_DONE != sqlite3_step(addStmt))
        {
            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));;
        }
        //  NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
        else
        {
            //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
            PlaceID = sqlite3_last_insert_rowid(database);
            NSLog(@"sqlite3_last_insert_rowid") ;
        }

        //Reset the add statement.
        sqlite3_reset(addStmt);
    }

干杯

答案 1 :(得分:0)

我认为你要找的是insertRowsAtIndexPaths:withRowAnimation。这将添加具有您选择的平滑动画的行。这仅适用于UITableViewDataSource已正确编码的情况。