Database updation issue
I am trying to update my database with some values using following code. But the values are not updated and it stored null to the same.
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];
}
In above code , no of the values are null still the database is storing null values after update. In insert it works perfect.
The Insert code is like (this is performed before update)
+(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) == SQLI开发者_运维百科TE_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];
}
}
Please help me to solve the issue.
Try calling sqlite3_finalize on your prepared statement.
精彩评论