数据库与 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;
}
在 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]); } }
When I'm going to open connect with database, console says: "error opening!: 14".
I included "mybase.sqlite" on folder Resources of my project and I'm using the FMDB framework.
For open connect I'm using this code:
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;
}
In AppDelegate, I included this code:
- (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]); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的开放路径可能不正确。您指定的路径没有意义,就好像您的数据库文件位于根文件夹中一样。
上面应该使用此代码作为文件路径,您在问题中已经有了该路径。
I think your open path is likely incorrect. You are specifying a path that doesn't make sense, as if your DB file was in the root folder.
The above should use this code for the file path, which you already have in the question.