我有一个调用网络服务并将结果保存到 sqlite 数据库的应用程序,其想法是当设备离线时,检索这些保存的结果。这一切正常,但如果我重建项目,数据库似乎被删除,我无法获取我之前输入的任何数据。有人能解释一下这里发生了什么吗?我正在使用 4.3 模拟器和 Xcode 4。
我也在使用 FMDatabase 库...
-(NSMutableArray*)retrieveDataSourceOfType:(NSString*)type{
FMDatabase *db = [self getDB];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:nil];
if([type isEqualToString:@"AlertItem"]){
FMResultSet *results = [db executeQuery:@"SELECT * FROM AlertItem"];
while([results next]){
AlertItem *tmp = [[AlertItem alloc] init];
tmp.Description = [results stringForColumn:@"Description"];
tmp.NumItems = [results intForColumn:@"NumItems"];
tmp.ModuleGroup = [results stringForColumn:@"ModuleGroup"];
tmp.StaffID = [results intForColumn:@"StaffID"];
tmp.Status = [results intForColumn:@"Status"];
[items addObject:tmp];
}
}
[db close];
[db release];
return items;
}
-(FMDatabase*)getDB{
NSString *path = [[[NSBundle mainBundle] resourcePath]
stringByAppendingString:@"/XXXX.sqlite"];
NSLog(@"%@", path);
NSFileManager *fm = [NSFileManager defaultManager];
if([fm fileExistsAtPath:path]){
FMDatabase *db = [[FMDatabase databaseWithPath:path]retain];
NSLog(@"%@", path);
if(![db open]){
[db release];
return nil;
}
else{
return db;
}
}
}
谢谢。
最佳答案
我认为这取决于您放置数据库文件的位置。如果它是一种资源,我认为每次部署项目时它都会被覆盖。您应该在应用程序缓存文件夹中复制/创建您的数据库。
获取路径的方法如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
Here是您可以使用的其他目录。
我会尝试 NSFileManager用于文件复制。我不确定这些我只使用 sqlite 进行自动完成,没有插入/更新。
尝试 fileExistsAtPath
查看文件是否存在,如果不存在,您应该使用 copyItemAtPath
关于iphone - iPhone 中的 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6979542/