1
我有一個SQLite數據庫爲我的應用程序。從db中檢索實體我使用這種方法:objective-c sqlite3數據庫更改不持久
- (sqlite3*) openDatabaseNamed:(NSString*)databaseName
{
if(![databaseName isEqualToString:kTopInternationalDatabaseName] &&
![databaseName isEqualToString:kTop500RODatabaseName]){
NSAssert(nil, @"Database does not exist!");
}
sqlite3 * dataBase;
NSString * path;
path = [[NSBundle mainBundle] pathForResource:databaseName ofType:@"sqlite3"];
if (sqlite3_open([path UTF8String], &dataBase) != SQLITE_OK) {
NSString * errorString = [NSString stringWithFormat:@"[SQLITE] Unable to open database <%@> ",databaseName];
NSAssert(nil,errorString);
}
return dataBase;
}
- (NSArray *) getAllEntitiesForDatabaseNamed:(NSString*)databaseName
{
(...)
sqlite3 * database = [self openDatabaseNamed:databaseName];
NSMutableArray *retval = [[NSMutableArray alloc] init];
NSString *query = [NSString stringWithFormat:@"SELECT * FROM %@",databaseName];
NSArray *properties = [entityClass classProperties];
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
for (int i=2; i<countLimit; i++){
chars = (char *) sqlite3_column_text(statement,i+1);
if(chars != NULL)
{
containerString = [NSString stringWithUTF8String:chars];
if(containerString && containerString.length>0){
[entityModel setValue:containerString forKey:properties[i]];
}
}
}
[retval addObject:entityModel];
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return retval.copy;
}
一切都按預期工作。爲了將實體的自定義字段設置爲數據庫中的特定值,我使用此方法:
- (void)setEntity:(EntityModel *)entity favorite:(BOOL)favorite
{
NSString *query = [NSString stringWithFormat:@"UPDATE %@ SET favorite = %i WHERE position = '%i';",kDatabaseName,favorite?1:0,entity.positionInTop];
sqlite3_stmt *statement;
sqlite3 * database = [self openDatabaseNamed:kTop500RODatabaseName];
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
sqlite3_step(statement);
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
發生的事情有點奇怪。如果我使用更新方法並且在使用getAllEntitiesForDatabaseNamed
的所有實體的應用查詢的同一生命週期中,我對setEntity:Favorite:
所做的更改仍然存在。另一方面,如果我使用更新方法,然後關閉應用程序並重新啓動它,則使用setEntity:Favorite:
所做的更改將丟失。任何想法是爲什麼?
PS:我也用sqlite3_exec
試過,仍然結果是相同的:
if (sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL) != SQLITE_OK) {
// deal with error...
NSLog(@" -- ERROR!");
}
你能再詳細一點嗎? – Teo
@Theo。查看代碼示例。 – Rob
我相信雖然斷言是錯誤的,並且它會在複製成功時使應用程序崩潰。 – Teo