Iphone 当应用程序在设备上运行时,如何获取Sqlite数据库文件?

Iphone 当应用程序在设备上运行时,如何获取Sqlite数据库文件?,iphone,sqlite,Iphone,Sqlite,在设备上运行数据库文件时,我在访问数据库文件时遇到问题。我怎样才能得到那个文件?在模拟器上运行应用程序时,访问文件并没有问题。就像我在模拟器上运行应用程序时,DB文件位于: /Users/Nitish/Library/Application Support/iPhone Simulator/4.3/Applications/1B787527-0608-4BC1-8022-DFDB3CC35F66/mysqlite.sqlite 当应用程序在设备上运行时,在哪里可以找到DB文件 签出数据库文件路

在设备上运行数据库文件时,我在访问数据库文件时遇到问题。我怎样才能得到那个文件?在模拟器上运行应用程序时,访问文件并没有问题。就像我在模拟器上运行应用程序时,DB文件位于:

/Users/Nitish/Library/Application Support/iPhone Simulator/4.3/Applications/1B787527-0608-4BC1-8022-DFDB3CC35F66/mysqlite.sqlite

当应用程序在设备上运行时,在哪里可以找到DB文件

签出数据库文件路径。这可能是唯一的原因

您的数据库很可能位于应用程序文档目录中。因此,您需要使用以下命令调用该目录:

- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

我使用此方法获取数据库的URL:

+ (NSURL *)storeURL
{
    if ([self isExecutingUnitTests])
    {
        NSString *directory = [[NSFileManager defaultManager] currentDirectoryPath];
        return [NSURL fileURLWithPath:[directory stringByAppendingPathComponent:@"UnitTests/test-database.sqlite"]];
    }
    else
    {
        return [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.sqlite"]];
    }
}
它在模拟器上、设备上甚至在执行单元测试时都能正常工作!
单元测试和文档都应该存在于.xcodeproj级别。

正如其他人所说,您的数据库不应该位于应用程序的根文件夹中。如果要从bundle加载静态数据库(即通过在xCode中添加预加载的数据库),则必须通过bundle访问它

NSString *path = [[NSBundle mainBundle] pathForResource:@"mysqlite" ofType:@"sqlite"];
如果您是从文档目录访问文件(即文件已被修改或保存)

@尼提斯语:

我有一个代码,当应用程序在设备上运行时,它将访问sqlite数据库文件

- (void) initDatabase
{

    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths =    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                        NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory
                            stringByAppendingPathComponent:@"contacts.db"];

    success = [fileManager fileExistsAtPath:writableDBPath];

    dbpath = [writableDBPath UTF8String];

    NSLog(@"path : %@", writableDBPath);

    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate
    //  location.

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
                           stringByAppendingPathComponent:@"contacts.db"];

    success = [fileManager fileExistsAtPath:defaultDBPath];
    //if(success) return;


    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success)
    {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.",[error localizedDescription]);
    }

    dbpath = [writableDBPath UTF8String];
    NSLog(@"path : %@", writableDBPath);
}
“NSLog()”
将为您提供sqlite数据库文件的路径。它适用于模拟器和设备。
希望它能对你有用。

因为我不完全理解你想做什么,这很有可能不会有什么帮助;如果这没有帮助,让我知道,我会删除它

如果您需要知道db是在哪里创建的,也许您可以在生产环境(即在某些iphone上)中运行应用程序时创建一个db,然后使用以下命令检索路径:


这应该适用于设备和模拟器。

阅读所有评论和答案,我认为实际上没有人回答作者的简单问题:

如何在实际设备上运行时访问sqlite数据库文件(如作者所述,这在iPhone模拟器中不是问题)

以下是我的工作:

  • (一次!)确保所有隐藏文件在Mac上都可见: 在终端窗口运行: 默认写com.apple.Finder appleshowall文件是

  • 在Xcode中,打开Organizer->Devices,查找您的iPhone,并从其“在应用程序中查找应用程序”文件夹中查找应用程序

  • 在底部单击下载并将应用程序下载到桌面(任何位置)

  • 打开Finder,导航到下载的文件,右键单击它并选择ShowPackageContents。该视图将更改为标准的finder视图,并在该应用程序中打开文件

  • 转到Documents文件夹,找到*.sqlite文件,右键单击它并选择Open With->Other

  • 选择SQLite Database Browser()并通过数据库浏览器查看数据库



  • 运行整个过程不到一分钟,只需执行一次。

    您如何在模拟器中访问数据库?上面的路径将我引导到sqlite文件。上面的路径将您引导到应用程序根目录。在该目录中,应该有一个
    文档
    目录以及您的应用程序包。我已经更新了路径。早些时候,我只是忘了在末尾写DB文件。但这是真实的情况下,模拟器只有我猜。如果我们在设备上工作,是否真的可以访问DB文件?您的路径表明您的DB位于应用程序根目录下。。这是错误的。。你是怎么做到的?您应该将其放在文档目录中。。。这就是您应该从中访问它的位置…当我在设备上运行应用程序时,这将返回sqlite文件的目录吗?这将返回Documents目录。您需要弄清楚如何使用该字符串插入数据库名称。通常如何获得该字符串?我不明白你的意思。
    [nsstringwithformat:@“%@/%@”,[self-applicationdocuments-directory],@“YourDatabase.db]首先,这是否为您提供了正确的结果?第二,我需要在我的数据库类中实现这个方法,我在数据库类中建立了DB连接,或者在其他地方?你在哪里实现这个方法?我使用了一个EntityManager类,它非常有用!这是它的名字和名字。谢谢Jenox。不幸的是,我身上没有这个设备。我明天一定要试试。谢谢。如果一切顺利,我会接受你的回答。嗨,我在获取sqlite数据库方面面临着不同的问题。我的开发人员配置文件证书过期,我的应用程序无法运行,但我无法重新安装。因为我想先获取数据,然后再移动。如何获取数据请指导我。此路径是否提供设备上的数据库?因为我想在sqlite浏览器中打开它。我想知道我该怎么做?啊,你是想用一个不在iphone上的应用程序从iphone打开数据库吗?这个应用程序在iphone上。假设我们正在调试iPhone应用程序。那么我们可以在sqlit浏览器中打开该应用程序的数据库吗?在我的情况下,文档文件夹也是空的,我认为Xcode不再复制sqlite文件,或者它已经在IOS上移动到另一个位置。
    
    - (void) initDatabase
    {
    
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths =    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                            NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *writableDBPath = [documentsDirectory
                                stringByAppendingPathComponent:@"contacts.db"];
    
        success = [fileManager fileExistsAtPath:writableDBPath];
    
        dbpath = [writableDBPath UTF8String];
    
        NSLog(@"path : %@", writableDBPath);
    
        if (success) return;
        // The writable database does not exist, so copy the default to the appropriate
        //  location.
    
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
                               stringByAppendingPathComponent:@"contacts.db"];
    
        success = [fileManager fileExistsAtPath:defaultDBPath];
        //if(success) return;
    
    
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success)
        {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.",[error localizedDescription]);
        }
    
        dbpath = [writableDBPath UTF8String];
        NSLog(@"path : %@", writableDBPath);
    }
    
    [someone@somewhere ~]$ sqlite3 foobar.db
    SQLite version 3.7.5
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> pragma database_list;
    0|main|/home/someone/foobar.db
    
    NSString *dbFilePath=[NSHomeDirectory stringByAppendingPathComponent:@"mysqlite.sqlite"];