11

「SQLite データベース ブラウザー」を使用して SQL データベースを作成し、それを Xcode プロジェクトにドラッグ アンド ドロップして、アプリをビルドしました。シミュレーターでは問題なく動作しますが、iPhone では次のエラーでクラッシュします。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
 reason: 'Failed to create writable database file with message 'The operation could‚ 
not be completed. (Cocoa error 260.)'.'   

これが私のコードです:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Creates a writable copy of the bundled default database in the application Documents directory:
    NSLog(@"AppDelegate...Looking for embedded Database file...");
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    // Grab the path to the Documents folder:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"users.sql"];

    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) {
        NSLog(@"Database File Exists in Documents folder!");
        NSLog(@"Its path is: %@", writableDBPath);
        return YES;
    }
    else {
    // But if the writable database does not exist, copy the default to the appropriate location.
    NSLog(@"!!NO Database File Exists in Documents folder!");
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"users.sql"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
    else 
        NSLog(@"WROTE THE DATABASE FILE!!!");
}

return YES;
}

繰り返しますが、これはシミュレーターでは機能しますが、iPhone では機能しません。(これは、".sqlite" 拡張子ではなく ".sql" 拡張子を持つファイルとは関係ありませんね? 原因は、"SQLite Database Browser" が作成するファイルに与える拡張子です.. .)

4

3 に答える 3

28

答えは、プロジェクトがそれを「見る」ように、SQLファイルの「ターゲットメンバーシップ」が適切に設定されていることを確認することに関係しています。

1) Xcode の左ペインで sql ファイルをクリックします。

2) File Inspector を開く/表示する (右側のペイン)

3) 「対象メンバーシップ」の下で、「チェック」が「オン」になっていることを確認します。

それでおしまい。

于 2012-04-30T13:03:03.933 に答える
2

ここに画像の説明を入力

この解決策は私の問題を解決します。

于 2014-04-21T12:11:57.140 に答える
0

私の問題では:この行を確認してください:

NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"users.sql"]

バンドルとコードのfileNameを確認してください。異なるファイル名の場合もエラーが発生します。

于 2012-10-02T06:11:50.953 に答える