Luc*_*tos 1 sqlite cocoa objective-c touch fmdb
当我要打开与数据库的连接时,控制台说:"错误打开!:14".我在项目的资源文件夹中包含了"mybase.sqlite",我正在使用FMDB框架.
对于开放式连接我正在使用此代码:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
FMDatabase* db = [FMDatabase databaseWithPath:@"/mybase.sqlite"];
if (![db open]) {
NSLog(@"Não abriu o banco de dados.");
[pool release];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在AppDelegate中,我包含了以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
// Override point for customization after application launch. HomeViewController *homeVC = [[HomeViewController alloc] init]; navigationController = [[UINavigationController alloc] initWithRootViewController:homeVC]; [self createEditableCopyOfDatabaseIfNeeded]; [window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES; }
- (void)createEditableCopyOfDatabaseIfNeeded{ BOOL success; NSFileManager
*fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString
*documentsDirectory = [paths objectAtIndex:0]; NSString
*writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"mybase.sqlite"]; success = [fileManager fileExistsAtPath:writableDBPath]; NSLog(@"Success %d", success); if (success) return; NSString
*defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mybase.sqlite"]; success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; if (!success) { NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); } }
Run Code Online (Sandbox Code Playgroud)
sly*_*rel 13
我认为你的开放路径可能不正确.您指定的路径没有意义,就像您的DB文件位于根文件夹中一样.
FMDatabase* db = [FMDatabase databaseWithPath:@"/mybase.sqlite"];
Run Code Online (Sandbox Code Playgroud)
上面应该使用此代码作为文件路径,您已在问题中使用.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"mybase.sqlite"];
FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7518 次 |
| 最近记录: |