将先前创建的 Sqlite 数据库写入 iOS 中的文档的最佳方法是什么

发布于 2024-12-11 20:21:39 字数 179 浏览 0 评论 0原文

我有一个先前创建的 Sqlite 数据库文件,我将其与 iOS 应用程序捆绑在一起。我的问题是将这些数据写入手机的最佳方法是什么,以便我可以从那里的应用程序更改表格?

我的研究表明,我可能需要循环遍历整个 .sql 文件并在手机上手动插入/创建必要的表和值。

有没有办法将数据库写入内部存储,然后再对其进行操作?

I have a previously created Sqlite database file that I am bundling with an iOS application. My question is what is the best way to write this data to the phone so that I may alter the tables from the application there on?

My research indicates that I may need to loop through the entire .sql file and insert/create the necessary tables and values on the phone manually.

Is there no way to just write the database to internal storage and then manipulate it from then on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2024-12-18 20:21:39

您可以将其添加到项目中的支持​​文件中,然后将该文件复制为模板数据库(如果不存在),然后将其打开。一旦你打开它,你就可以用它做你想做的事情。

这是我执行此操作的示例项目中的一些示例代码。在我的示例中,它是一个联系人数据库。请注意,如果包不存在,ensurePrepared 将如何从包中复制它。我复制到库路径,但您可以复制到文档路径。

- (BOOL)ensureDatabaseOpen: (NSError **)error
{
    // already created db connection
    if (_contactDb != nil)
    {
        return YES;
    }

    NSLog(@">> ContactManager::ensureDatabaseOpen");    
    if (![self ensureDatabasePrepared:error])
    {
        return NO;
    }

    const char *dbpath = [_dbPath UTF8String]; 
    if (sqlite3_open(dbpath, &_contactDb) != SQLITE_OK &&
        error != nil)
    {
        *error = [[[NSError alloc] initWithDomain:@"ContactsManager" code:1000 userInfo:nil] autorelease];
        return NO;
    }

    NSLog(@"opened");

    return YES;
}

- (BOOL)ensureDatabasePrepared: (NSError **)error
{
    // already prepared
    if ((_dbPath != nil) &&
        ([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
    {
        return YES;
    }

    // db in main bundle - cant edit.  copy to library if !exist
    NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
    NSLog(@"%@", dbTemplatePath);

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    _dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];

    NSLog(@"dbPath: %@", _dbPath);

    // copy db from template to library
    if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
    {
        NSLog(@"db not exists");
        NSError *error = nil;
        if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
        {
            return NO;
        }

        NSLog(@"copied");
    }    

    return YES;    
}

You can add it to your supporting files in the project and then copy the file as a template db if it doesn't exist and then open it. Once you open it you can do what you want with it.

Here's some sample code from a sample project I had that does this. In my sample it's a contact database. Notice how ensurePrepared will copy it from the bundle if it doesn't exist. I copy to library path but you can copy to documents path.

- (BOOL)ensureDatabaseOpen: (NSError **)error
{
    // already created db connection
    if (_contactDb != nil)
    {
        return YES;
    }

    NSLog(@">> ContactManager::ensureDatabaseOpen");    
    if (![self ensureDatabasePrepared:error])
    {
        return NO;
    }

    const char *dbpath = [_dbPath UTF8String]; 
    if (sqlite3_open(dbpath, &_contactDb) != SQLITE_OK &&
        error != nil)
    {
        *error = [[[NSError alloc] initWithDomain:@"ContactsManager" code:1000 userInfo:nil] autorelease];
        return NO;
    }

    NSLog(@"opened");

    return YES;
}

- (BOOL)ensureDatabasePrepared: (NSError **)error
{
    // already prepared
    if ((_dbPath != nil) &&
        ([[NSFileManager defaultManager] fileExistsAtPath:_dbPath]))
    {
        return YES;
    }

    // db in main bundle - cant edit.  copy to library if !exist
    NSString *dbTemplatePath = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"db"];
    NSLog(@"%@", dbTemplatePath);

    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    _dbPath = [libraryPath stringByAppendingPathComponent:@"contacts.db"];

    NSLog(@"dbPath: %@", _dbPath);

    // copy db from template to library
    if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
    {
        NSLog(@"db not exists");
        NSError *error = nil;
        if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:&error])
        {
            return NO;
        }

        NSLog(@"copied");
    }    

    return YES;    
}
相权↑美人 2024-12-18 20:21:39

将数据库存储在捆绑包中(在项目中)。在应用程序委托中,将数据库复制到文档目录(仅当文档目录中尚不存在数据库文件时才执行此操作,否则您将始终覆盖您的文件)。在您的代码中始终引用文档目录中的副本。
另请阅读这个SO问题获取一些代码关于如何进行副本。

瞧。

Store the database in the bundle (in the project). In the App Delegate copy the database to the Documents directory (only do this if the database file doesn't already exist in the documents directory otherwise you will always be overwriting your file). In your code always reference the copy in the Document's directory.
Also read this SO Question for some code on how to do the copy.

Voila.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文