我在我的应用程序中通过FMDB使用SQLite,并且它在过去几个月一直在所有XCode模拟器上工作,但是,当我现在在实际设备上进行测试时,我似乎遇到了问题当我尝试插入时。
我得到的错误是:
error domain = fmdatabase code = 14"无法打开数据库文件" UserInfo = 0x178665340 {NSLocalizedDescription =无法打开数据库文件}
这是我的代码:
我将所有FMDB内容包装在我创建的Helper类中:
_lHelper = [[BTRLocationsDatabaseHelper alloc] init];
self.dbString = [_lHelper checkTableAndCreate:TABLE_LOCAL_LOCATIONS];
那叫:
NSString *const LOCATIONS_DB_PATH = @"/Library/Caches/myapp_locations.db";
NSString *const TABLE_LOCAL_LOCATIONS = @"locallocations";
NSString *const TABLE_FAVORITE_LOCATIONS = @"favoritelocations";
- (instancetype)init {
self = [super init];
if(self) {
_db = [FMDatabase databaseWithPath:LOCATIONS_DB_PATH];
if(![_db open]) {
NSLog(@"Error opening myapp_locations database");
}
}
return self;
}
-(NSString *)checkTableAndCreate:(NSString *)table {
NSString *errorString = @"";
FMResultSet *rs = [self.db executeQuery:@"select DISTINCT tbl_name from sqlite_master where tbl_name = ?", table];
[rs next];
BOOL errors = NO;
if([rs hasAnotherRow]) {
// table exists - I think -> So here we would do nothing
NSLog(@"locallocations table existed");
} else {
NSLog(@"locallocations table does not exist");
// table does not exist - I think -> So here we would create the table
if([table isEqualToString:TABLE_LOCAL_LOCATIONS]) {
NSLog(@"Creating a new locallocations table");
BOOL success = [self.db executeUpdate:@"create table locallocations .....)"];
if(!success) {
errors = YES;
NSString *err = [NSString stringWithFormat:@"%@", [self.db lastError]];
[errorString stringByAppendingString:err];
NSLog(@"Error create table locallocations: %@", [self.db lastError]);
}
}
}
FMResultSet *rsFavorites = [self.db executeQuery:@"select DISTINCT tbl_name from sqlite_master where tbl_name = ?", @"favoritelocations"];
[rsFavorites next];
BOOL errorsFavorites = NO;
if([rsFavorites hasAnotherRow]) {
// table exists - I think -> So here we would do nothing
NSLog(@"favoritelocations table existed");
} else {
NSLog(@"favoritelocations table does not exist");
NSLog(@"Creating a new favoritelocations table");
// table does not exist - I think -> So here we would create the table
BOOL success = [self.db executeUpdate:@"create table favoritelocations ...."];
if(!success) {
errorsFavorites = YES;
NSString *err = [NSString stringWithFormat:@", %@", [self.db lastError]];
[errorString stringByAppendingString:err];
NSLog(@"Error create table favoritelocations: %@", [self.db lastError]);
}
}
// Down here we would do logic for adding new columns if we ever need them - if errors is false
return errorString;
}
在完成之后我打印出errorString并且它是空的,这让我相信表创建工作。
然后我稍后尝试在Helper类中调用此方法:
-(NSString *)insertFavoriteLocation:(BTRLocation *)location
userId:(int)userId {
NSMutableDictionary *dictionaryArgs = [NSMutableDictionary dictionary];
//... just adding stuff to my dictionary
BOOL result = [self.db executeUpdate:@"insert into favoritelocations ....];
NSError *error = [self.db lastError];
NSString *errorString = [NSString stringWithFormat:@"%@", error];
return errorString;
}
运行之后我打印出errorString,它说上面粘贴的错误,错误代码14无法打开数据库文件。
所以这在模拟器上完美运行,可能导致它无法在设备上运行(iPhone 5s具体,但这是我尝试过的唯一设备)。
谢谢!
修改
NSString* path = [NSSearchPathForDirectoriesInDomains(
NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
解决方案:
对于那些关心的人,这就是我最终修复它的方式:
修改了init方法:
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(
NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [cachePath stringByAppendingPathComponent:@"myapp_locations.db"];
_db = [FMDatabase databaseWithPath:dbPath];