我正在使用SQLite数据库,我有一个名为tblUser的表,其中我有变量说Id,name,image,gender。
在我的应用程序中,我提供添加,删除更新用户条目(通用功能)的功能。假设我在具有2个用户的页面上默认有2条记录并且两者都被删除。现在插入的第3条记录将提供主键为3。
现在,如果我编辑该特定记录,那么它不会使用新数据进行更新,而是当我调用以下函数时,相同的主键将替换为1:
+ (void) getInitialDataToDisplay:(NSString *)dbPath
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//sqlite3_open([dbPath UTF8String], &database);
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select UserId,UserAge,UserName,UserGender,UserImage from [tblUser]";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSInteger primaryKey = sqlite3_column_int(selectstmt,0);
UserDetail *DataObj = [[UserDetail alloc] initWithPrimaryKey:primaryKey];
DataObj.UserAge = sqlite3_column_int(selectstmt,1);
DataObj.UserName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
DataObj.UserGender = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
DataObj.UserImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
DataObj.isDirty = NO;
[appDelegate.DataArray addObject:DataObj];
}
}
else
{
NSAssert1(0,@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
请注意,当我回到显示用户列表的页面后,我正在调用此函数,此页面点击了需要填写所有详细信息的页面的保存按钮。任何提示都会有所帮助。谢谢。