简体   繁体   English

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

[英]What is the best way to write a previously created Sqlite database to documents in iOS

I have a previously created Sqlite database file that I am bundling with an iOS application. 我有一个先前创建的Sqlite数据库文件,该文件与iOS应用程序捆绑在一起。 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. 我的研究表明,我可能需要遍历整个.sql文件,并在手机上手动插入/创建必要的表和值。

Is there no way to just write the database to internal storage and then manipulate it from then on? 有没有办法只将数据库写入内部存储,然后再对其进行操作?

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. 请注意,如果ensurePrepared不存在,它将如何从捆绑软件中复制它。 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;    
}

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. 另请阅读此SO问题以获取有关如何执行复制的一些代码。

Voila.

Include the .sql file in your resource folder of the project and copy it into user's document folder on the first launch of your application(or if the file is not found). 将.sql文件包括在项目的资源文件夹中,并在应用程序首次启动时将其复制到用户的文档文件夹中(如果找不到该文件)。 You can use NSFileManager for this. 您可以为此使用NSFileManager。

Take a look at Where would you place your SQLite database file in an iPhone app? 看看您将SQLite数据库文件放在iPhone应用程序中的什么位置?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:在iOS中,处理数据库的最佳方法是什么? - iOS: In iOS what is the best way to deal with a database? iPhone的任何接口来处理在应用程序的Documents文件夹中创建的sqlite数据库? - iphone any interface to deal with sqlite database created in the application Documents folder? 我应该将sqlite数据库文件写入Documents目录还是Library / Caches? - Should i write sqlite database file to Documents directory or Library/Caches? 在应用程序内部使用SQLite数据库的最佳方法 - Best way to work with SQLite database inside application 在SQLite中保存数组的最佳方法是什么? - What would be the best way to save arrays in SQLite? 备份iPhone应用程序sqlite3数据库数据的最佳方法是什么? - what is the best way to take the backup of data of sqlite3 database of iphone application? 编写混合iPhone应用程序的最佳方法是什么? - What is the best way to write a hybrid iPhone app? 检查数据库中现有数据的最佳方法(iOS) - Best way to check existent data in the database (iOS) 在iPhone上找到用户的Documents目录的最佳方法是什么? - What's the best way to find the user's Documents directory on an iPhone? 在iOS上查看大型PDF的最佳方式是什么? - What is the best way to view large PDFs on iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM