您将SQLite数据库文件放在iPhone应用程序中的哪个位置? [英] Where would you place your SQLite database file in an iPhone app?

查看:112
本文介绍了您将SQLite数据库文件放在iPhone应用程序中的哪个位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我最初为我的应用程序创建带有预先插入数据集的SQLite数据库文件时,我必须将此文件放在我的Xcode项目中的某个位置,以便它转到我的iPhone应用程序。我猜ressources是正确的选择。

When I initially create an SQLite database file with pre-inserted datasets for my application, I would have to place this file somewhere in my Xcode project so that it goes to my iPhone application. I guess "ressources" is the right place for that.

在iPhone应用程序中部署SQLite数据库文件的基本步骤是什么?

What are the basic "steps" for deployment of an SQLite database file in an iPhone application?


  • 手动创建数据库

  • 将数据库文件添加到项目中(其中?)

我目前正在阅读整个SQLite文档,尽管与iPhone不太相关。

I'm currently reading the whole SQLite documentation, although that's not much iPhone related.

推荐答案

您需要先将SQLite文件添加到Xcode项目中 - 最合适的位置是资源文件夹。

You need to add the SQLite file to your Xcode project first - the most appropriate place is in the resources folder.

然后在你的应用程序委托代码文件,在appDidFinishLaunching方法中,您需要首先检查是否已创建SQLite文件的可写副本 - 即:已在iPhone文件系统上的用户文档文件夹中创建了SQLite文件的副本。如果是,你不做任何事情(否则你会用默认的Xcode SQLite副本覆盖它)

Then in your application delegate code file, in the appDidFinishLaunching method, you need to first check if a writable copy of the the SQLite file has already been created - ie: a copy of the SQLite file has been created in the users document folder on the iPhone's file system. If yes, you don't do anything (else you would overwrite it with the default Xcode SQLite copy)

如果不是,那么你将SQLite文件复制到那里 - 来制作它是可写的。

If no, then you copy the SQLite file there - to make it writable.

请参阅下面的代码示例来执行此操作:这是从Apple的SQLite书籍代码示例中获取的,其中此方法是从应用程序委托appDidFinishLaunching方法调用的。 / p>

See the below code example to do this: this has been taken from Apple's SQLite books code sample where this method is called from the application delegates appDidFinishLaunching method.

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success)
        return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

=========== =

============

以下是Swift 2.0中的上述代码+

Here's the above code in Swift 2.0+

// Creates a writable copy of the bundled default database in the application Documents directory.
private func createEditableCopyOfDatabaseIfNeeded() -> Void
{
    // First, test for existence.
    let fileManager: NSFileManager = NSFileManager.defaultManager();
    let paths:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentsDirectory:NSString = paths.objectAtIndex(0) as! NSString;
    let writableDBPath:String = documentsDirectory.stringByAppendingPathComponent("bookdb.sql");

    if (fileManager.fileExistsAtPath(writableDBPath) == true)
    {
        return
    }
    else // The writable database does not exist, so copy the default to the appropriate location.
    {
        let defaultDBPath = NSBundle.mainBundle().pathForResource("bookdb", ofType: "sql")!

        do
        {
            try fileManager.copyItemAtPath(defaultDBPath, toPath: writableDBPath)
        }
        catch let unknownError
        {
            print("Failed to create writable database file with unknown error: \(unknownError)")
        }
    }
}

这篇关于您将SQLite数据库文件放在iPhone应用程序中的哪个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆