数据库更新问题

时间:2011-10-07 14:53:28

标签: iphone ios4 sqlite

我正在尝试使用以下代码使用某些值更新我的数据库。但是值不会更新,并且存储为null。

int NoOfItem = [str_NoOfitems integerValue];
int FurnitureId = [str_FurnitueId integerValue];
NSLog(@"FurnitureId %@", str_FurnitueId);
@try {
    if (sqlite3_open([[SQLDataFile getDBPath] UTF8String], &database) == SQLITE_OK)
    {

        if(updateStmt == nil) 
        {
            NSString *sql = [NSString stringWithFormat:@"UPDATE FurnitureSelected Set  NoOfItem = ? Where FurnitureId = %d" ,    FurnitureId];

            if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &updateStmt, NULL) != SQLITE_OK)
            {
                NSLog(@"erroe while creating Update statement  %s",sqlite3_errmsg(database) );

            }               
        }

//      sqlite3_bind_text(updateStmt, 3, [str_NoOfitems UTF8String], -1, SQLITE_TRANSIENT);

        sqlite3_bind_int(updateStmt, 3, NoOfItem);
        if(SQLITE_DONE != sqlite3_step(updateStmt))
            NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));



    }

}
@catch (NSException * e) {
    NSLog(@"Exception occured while Updating datbase for FurnitureSelected %@", [e description]);
}
@finally
{
    [SQLDataFile finalizeStatements];
}

在上面的代码中,没有值为null仍然是数据库在更新后存储空值。在插入中它完美无缺。

插入代码就像(在更新前执行)

+(void)InsertFurnitureValues:(NSString *)str_FurnitureId:(NSString *)str_NoOfItems:(NSString *)str_RoomId {

int noOfItem = [str_NoOfItems integerValue];
int FId = [str_FurnitureId integerValue];
@try 
{

    if (sqlite3_open([[SQLDataFile getDBPath] UTF8String], &database) == SQLITE_OK)
    {

        if(addStmt == nil) 
        {
            const char *sql = "insert into FurnitureSelected(AutoId,RoomId, FurnitureId, NoOfItem) Values(?,?, ?,?)";
            if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
                NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
        }

        sqlite3_bind_int(addStmt, 3, FId);//sqlite3_bind_text(addStmt, 2, [str_RoomId UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 2, [str_RoomId UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(addStmt, 4, noOfItem);//(addStmt, 4, [str_NoOfItems UTF8String], -1, SQLITE_TRANSIENT);


        if(SQLITE_DONE != sqlite3_step(addStmt))
            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));


    sqlite3_reset(addStmt);

    }

}
@catch (NSException * e) 
{
    NSLog(@"Exception occure %@" , [e description]);
}
@finally 
{
    [SQLDataFile finalizeStatements];
}

}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

尝试在准备好的声明中调用sqlite3_finalize。