gpt4 book ai didi

objective-c - 数据库与 FMDB 打开连接时出错

转载 作者:行者123 更新时间:2023-12-03 16:49:03 25 4
gpt4 key购买 nike

当我要打开与数据库的连接时,控制台显示:“打开错误!: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]); } }

最佳答案

我认为您的开放路径可能不正确。您指定的路径没有意义,就好像您的数据库文件位于根文件夹中一样。

FMDatabase* db = [FMDatabase databaseWithPath:@"/mybase.sqlite"];

上面应该使用此代码作为文件路径,您在问题中已经有了。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"mybase.sqlite"];
FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];

关于objective-c - 数据库与 FMDB 打开连接时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4338834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com