2011-01-27 9 views
1

私は新しいiPhoneプロジェクトに取り組んでおり、sqliteに問題があります。これまで私は別のプロジェクトでこれをやっていましたが、うまくいきましたので、これで何が起こっているのか正確には分かりません。前と同じコードを使用していますが、状況は少し異なります。iPhoneシミュレータを使用したSQLiteエラー/可能な権限の問題

まず、ユニットテストを使用してCocoaユニットテストバンドルを作成しましたが、正しく動作していれば、sqliteデータベースのユニットテストを行いたいと思っていました。

このテストを実行する最初の事は次のようである[自己checkAndCreateDatabase]です:

-(void) checkAndCreateDatabase{ 
     BOOL success; 

     // Create a FileManager object, we will use this to check the status 
     // of the database and to copy it over if required 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     // Check if the database has already been created in the users filesystem 
     success = [fileManager fileExistsAtPath:databasePath]; 

     // If the database already exists then return without doing anything 
     if(success) return; 

     // If not then proceed to copy the database from the application to the users filesystem 

     // Get the path to the database in the application package 
     NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

     // Copy the database from the package to the users filesystem 
     [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 

     [fileManager release]; 
    } 

その後、私は次の行でデータベースを開こう:

int result = sqlite3_open([databasePath UTF8String], &database); 

これは毎回失敗エラーコードは14 SQLITE_CANTOPEN、databasePathは "/ Users/labuser/Library/Application Support/iPhone Simulator/Documents/projectfusion.db3"です。

奇妙なのは、私がそのディレクトリに移動すると、Documents/isは存在しないため、作成した場合は失敗しませんが、projectfusion.db3のサイズは0kbです。テーブルはそこにはありません。これは、テーブルが存在しないため、sqlite3_prepare_v2()に失敗します。 projectfusion.db3ファイルを実行する前にそのディレクトリに手動でコピーしても問題ありません。

私は単体テストの中でこれをやっているのですか?スクリプトには許可がありませんか?それとも、私が大学の学校のコンピュータで働いていて、そのディレクトリに書き込めないためですか? (私は管理者としてログインしようとしたが、どちらもうまくいかなかった)。

答えて

0

を使用してそのパスを取得することができます。

 
- (NSString *)createEditableCopyOfDatabaseIfNeeded { 
    // First, test for existence. 
    BOOL success; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    #ifdef TEST 
    documentsDirectory = [[NSFileManager defaultManager] currentDirectoryPath]; 
    #endif 


    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DBNAME]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) { return writableDBPath;} ; 
    // The writable database does not exist, so copy the default to the appropriate location. 

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DBNAME];  
    #ifdef TEST 
    defaultDBPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"YOURDB" ofType:@"sqlite"]; 
    #endif 

    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
    return (defaultDBPath); 
} 
0

xcodeでアプリケーションバンドルにデータベースを追加し、アプリケーションのDocumentsディレクトリにコピーしてみてください。あなたは、私がコードをコメント維持する必要はありませんので、私はTESTと呼ばれるマクロを設定して、

を下記のコードを試してみてください

- (NSString *)applicationDocumentsDirectory { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
return basePath; 
} 
+0

なぜ私はすでに行っていることと違うのか分かりません。私は上記の私のコードを上記のNSString * databasePathFromApp = [NSString stringWithFormat:@ "%@ /%@"、[self applicationDocumentsDirectory]、databaseName]と置き換えて試しました。 と返されたパスは、上記と同じ "/ Users/labuser/Library/Application Support/iPhone Simulator/Documents/projectfusion.db3"でした。 「アプリケーションバンドルにデータベースを追加する」とは、既存のファイルとして追加することを意味しますか?私は "Resources"ディレクトリにそれをしました。 – gameburke

+0

これは次のようにsmthを返します。/ Users/max/Library/Application Support/iPhone Simulator/4.2/Applications/C5178257-E12C-4088-BA2C-68E472458575/Documentsこれは本当に奇妙です。あなたはアプリケーションバンドルからあなたのDBにアクセスできますか? – Max

+0

私はFinderのbuild/Debug-iphonesimulator/ProjectFusionの中でProjectFusionの "Show Package Contents"を見て、それがそこにありました。それはあなたが意味することですか? – gameburke

関連する問題