2009-04-04 9 views
32

アプリケーションのデータセットがあらかじめ組み込まれたSQLiteデータベースファイルを最初に作成すると、このファイルを自分のXcodeプロジェクトのどこかに配置して、自分のiPhoneアプリケーションに移動する必要があります。私は "ressources"がそのための適切な場所だと思います。iPhoneアプリケーションにSQLiteデータベースファイルを置くのはどこですか?

iPhoneアプリケーションにSQLiteデータベースファイルを展開するための基本的な手順は何ですか?プロジェクトにデータベースファイルを追加し、手動で

  • データベースを作成

    • (?)

    それはあまりiPhone関連していないのですが、私は現在、全体のSQLiteのドキュメントを読んでいます。

  • 答えて

    70

    あなたのXcodeプロジェクトにSQLiteファイルを追加する必要があります。最も適切な場所はresourcesフォルダです。

    次に、アプリケーションデリゲートコードファイルのappDidFinishLaunchingメソッドで、SQLiteファイルの書き込み可能なコピーが既に作成されているかどうかを確認する必要があります。つまり、SQLiteファイルのコピーがユーザーで作成されているドキュメントフォルダをiPhoneのファイルシステムに保存します。もしそうなら、あなたは何もしません(そうでなければ、デフォルトのXcode SQLiteコピーで上書きします)。

    もしそうでなければ、そこでSQLiteファイルをコピーして書き込み可能にします。

    これを行うには、次のコード例を参照してください。これは、アプリケーションのdelegates appDidFinishLaunchingメソッドから呼び出されるAppleのSQLiteブックのコードサンプルから取得されています。あなただけのデータを照会するつもりなら

    // 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]); 
        } 
    } 
    

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

    はここスウィフト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)") 
         } 
        } 
    } 
    
    +0

    コードを取得したページからどうぞよろしいですか?ありがとう。 – itsaboutcode

    +0

    どこに書きたいのですか?私はそれがResoursesグループ(メインバンドル)にある場合、それは読み取り専用であることを知っています。したがって、書き込みと読み取りの両方を許可する正しい方法ですか?ありがとう! –

    +0

    これはちょっと遅かったのですが、メインバンドルからアップデートする必要がある場合は、どうすれば見つけられますか?私はファイルの属性をチェックしようとしましたが、私は新しいバージョンをコンパイルするたびに、アプリケーション内のファイルの変更日が変更されます。 – Flipper

    6

    で上記のコードですが、あなたはそれをメインバンドルに残すことができるはずです。

    しかし、これはおそらく良い例ではありません。あなたが将来データベースの書き込みを可能にするためにあなたのアプリケーションを拡張するならば、あなたは再びすべてを理解しなければならないでしょう...

    +3

    @ウィリー 必ずしもそうである必要はありません。将来的に彼のアプリケーションを拡張する場合は、常に上記の機能をアップデートに追加するだけです。シームレスに動作するはずです。 –

    関連する問題