【问题标题】:How to get Sqlite database file when application is running on device?应用程序在设备上运行时如何获取 Sqlite 数据库文件?
【发布时间】:2011-07-08 06:28:55
【问题描述】:

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

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

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

【问题讨论】:

  • 你是如何访问模拟器中的数据库的?
  • 上面的路径引导我到 sqlite 文件。
  • 上面的路径将您引导到应用程序根目录。在该目录中,应该有一个 Documents 目录以及您的应用程序包。
  • 我已经更新了路径。早些时候我只是忘了最后写数据库文件。但这仅在我猜的模拟器的情况下是正确的。如果我们在设备上工作,是否真的可以访问数据库文件?
  • 你的路径表明你的数据库在你的应用程序根目录下。这是错误的。你是怎么把你的数据库弄到那里的?你应该把它放在文档目录中......这就是你应该从那里访问它......

标签: iphone sqlite


【解决方案1】:

阅读所有cmets并回答,我认为实际上没有人回答作者的简单问题:

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

这是我的工作:

  1. (一次!)确保所有隐藏文件在您的 Mac 上可见: 在终端窗口运行: 默认写入 com.apple.Finder AppleShowAllFiles YES

  2. 在 Xcode 中,打开 Organizer -> Devices,找到您的 iPhone,然后在 Applications 文件夹中找到您的应用程序。

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

  4. 打开 Finder,导航到下载的文件,右键单击它并选择显示包内容。该视图将更改为标准取景器视图,并在该应用中打开文件。

  5. 转到 Documents 文件夹,找到您的 *.sqlite 文件,右键单击它并选择打开方式 -> 其他

  6. 选择 SQLite 数据库浏览器 (http://sqlitebrowser.sourceforge.net) 并通过数据库浏览器查看您的数据库。

运行一次,运行整个过程不到一分钟。

【讨论】:

  • 在我的情况下,文档文件夹也是空的,我认为 Xcode 不再复制 sqlite 文件或者它已在 IOS 上移动到另一个位置。
【解决方案2】:

我使用这种方法获取数据库的 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"]];
    }
}

它在模拟器、设备上甚至在执行单元测试时都可以正常工作! UnitTests 和 Documents 都应该存在于 .xcodeproj 的级别上。

【讨论】:

  • 你在哪里实现这个方法?
  • 我使用了一个非常有用的 EntityManager 类!这是它的headerimplementation
  • 感谢 Jenox。不幸的是,我没有随身携带设备。我明天一定会试试的。非常感谢。如果一切正常,我会接受你的回答。
  • 嗨,我在获取 sqlite 数据库时遇到了不同的问题。我的开发者资料证书已过期,我的应用程序无法运行,但我无法重新安装它。因为我想先获取该数据,然后再移动。如何获取这些数据,请指导我。
【解决方案3】:

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

NSString *path = [[NSBundle mainBundle] pathForResource:@"mysqlite" ofType:@"sqlite"];

如果您从文档目录访问文件(即它已被修改或保存)。

NSString *fileName = @"mysqlite.sqlite";
NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *path = [directoryPath stringByAppendingPathComponent:fileName];

【讨论】:

  • 此路径是否提供设备上的数据库?因为我想在 sqlite 浏览器中打开它。我想知道我该怎么做?
  • 啊,您是否尝试使用不在 iPhone 上的应用程序从 iphone 打开数据库?
  • 应用程序在 iPhone 中。假设我们正在调试 iPhone 应用程序。那我们可以在 sqlit 浏览器中打开那个应用程序的数据库吗?
【解决方案4】:

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

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

【讨论】:

  • 当我在设备上运行我的应用程序时,这会返回我的 sqlite 文件的目录吗?
  • 这将返回 Documents 目录。您需要弄清楚如何使用该字符串插入数据库名称。
  • 你通常会怎么做?我不明白你的意思。
  • [NSString stringWithFormat:@"%@/%@",[self applicationDocumentsDirectory], @"YourDatabase.db"];
  • 首先,这会给你正确的结果吗?其次,我需要在我已经建立数据库连接的数据库类或其他地方实现此方法?
【解决方案5】:

@nitish:

我有一个代码可以在应用程序在设备上运行时访问 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 数据库文件的路径。它适用于模拟器和设备。 希望它对你有用。

【讨论】:

    【解决方案6】:

    由于我不完全理解您要做什么,因此很有可能这不会很有帮助;如果这没有帮助,请告诉我,我会删除它。

    如果您需要知道创建数据库的位置,也许您可​​以在生产环境中运行应用程序时创建一个数据库(即在某些 iphone 上),然后使用 database_list pragma 检索路径:

    [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
    

    【讨论】:

      【解决方案7】:

      检查您的数据库文件路径。这可能只是原因。

      【讨论】:

        【解决方案8】:

        NSString *dbFilePath=[NSHomeDirectory stringByAppendingPathComponent:@"mysqlite.sqlite"];
        

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

        【讨论】:

          猜你喜欢
          • 2012-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-27
          • 2014-09-24
          • 1970-01-01
          相关资源
          最近更新 更多