Iphone 我们能在Xcode中给出sqlite的硬编码路径吗?

Iphone 我们能在Xcode中给出sqlite的硬编码路径吗?,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我对iPhone开发和Mac操作系统还不熟悉,请接受这个愚蠢的问题。但我努力深入研究,却找不到解决问题的办法 我已经通过命令提示符在sqlite中创建了一个数据库。数据库保存在Users/DNamto/resources.db中 但是当我试图用下面的代码片段在我的iPhone应用程序中打开这个db时 // Get the documents directory dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirecto

我对iPhone开发和Mac操作系统还不熟悉,请接受这个愚蠢的问题。但我努力深入研究,却找不到解决问题的办法

我已经通过命令提示符在sqlite中创建了一个数据库。数据库保存在Users/DNamto/resources.db中

但是当我试图用下面的代码片段在我的iPhone应用程序中打开这个db时

// Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc]initWithString: [docsDir stringByAppendingPathComponent:@"resources.db"]];
- (void)copyDatabaseIfNeeded {

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:[self getDBPath]];
    NSString *databasePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbname.sqlite"];
    if(success)
    {
        return;// remove old one.
    }
    [fileManager copyItemAtPath:databasePath toPath:[self getDBPath] error:nil];
}
-(void)openDatabase
{
    @try
    {
        [self copyDatabaseIfNeeded];
        if(sqlite3_open([[self getDBPath] UTF8String], &mainDatabase)==SQLITE_OK)
        {
            NSLog(@"Database opened");
        }
    }
    @catch (NSException *exception)
    {
        NSLog(@"Exception in openDatabase %@ :%@",exception.name,exception.reason);
    }
}

- (NSString *)getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"dbname.sqlite"];
}
数据库无法打开。 应用程序正在搜索的数据库路径为: /Users/DNamto/Library/Application Support/iPhone Simulator/6.0/Applications/C82C3DAF-4E95-49A7-9A4F-4D69B056DC9D/Documents/resources.db

有人能帮我找到正确的数据库路径吗。
我们可以硬编码数据库路径,以便我的应用程序链接到它。如果是,请提供代码片段

你不能。在实际设备中,您无法获取硬编码路径。

您需要相对路径。

这里您的问题是您的数据库不在文档目录中

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"resource.db"];
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"resource.db"];
NSError *error;
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:folderPath error:&error];
您需要将数据库添加到主捆绑包中,在运行时您需要检查数据库是否存在于文档目录中,如果不存在,您需要使用
NSFileManager将其复制到文档目录中。

您可以使用以下代码将数据库文件从bundle复制到document目录

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"resource.db"];
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"resource.db"];
NSError *error;
[[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:folderPath error:&error];

在应用程序中添加数据库&检查数据库是否存在于doc目录中,如果不存在,则需要将其复制到doc目录中,然后访问它。 对于cppy,doc目录中的db使用以下代码段

// Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc]initWithString: [docsDir stringByAppendingPathComponent:@"resources.db"]];
- (void)copyDatabaseIfNeeded {

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:[self getDBPath]];
    NSString *databasePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbname.sqlite"];
    if(success)
    {
        return;// remove old one.
    }
    [fileManager copyItemAtPath:databasePath toPath:[self getDBPath] error:nil];
}
-(void)openDatabase
{
    @try
    {
        [self copyDatabaseIfNeeded];
        if(sqlite3_open([[self getDBPath] UTF8String], &mainDatabase)==SQLITE_OK)
        {
            NSLog(@"Database opened");
        }
    }
    @catch (NSException *exception)
    {
        NSLog(@"Exception in openDatabase %@ :%@",exception.name,exception.reason);
    }
}

- (NSString *)getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"dbname.sqlite"];
}
要打开数据库,请使用以下代码段

// Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc]initWithString: [docsDir stringByAppendingPathComponent:@"resources.db"]];
- (void)copyDatabaseIfNeeded {

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:[self getDBPath]];
    NSString *databasePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbname.sqlite"];
    if(success)
    {
        return;// remove old one.
    }
    [fileManager copyItemAtPath:databasePath toPath:[self getDBPath] error:nil];
}
-(void)openDatabase
{
    @try
    {
        [self copyDatabaseIfNeeded];
        if(sqlite3_open([[self getDBPath] UTF8String], &mainDatabase)==SQLITE_OK)
        {
            NSLog(@"Database opened");
        }
    }
    @catch (NSException *exception)
    {
        NSLog(@"Exception in openDatabase %@ :%@",exception.name,exception.reason);
    }
}

- (NSString *)getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"dbname.sqlite"];
}
使用以下代码段关闭数据库

-(void)closeDatabase:(sqlite3_stmt*)statement
{
    @try
    {
        sqlite3_finalize(statement);
        sqlite3_close(mainDatabase);
    }
    @catch (NSException *exception)
    {
        NSLog(@"Exception in DatabaseController closeDatabase %@ :%@",exception.name,exception.reason);
    }
}

您必须首先在document文件夹中复制数据库,然后打开iThanks Girish nad Midhun MP以获得快速响应。不要在方法前加前缀w/
get
。应该是
dbPath
databasePath