2012-01-24 11 views
0

私のアプリケーションがシミュレータ上で完璧に動作するようになりましたが、iPhoneにロードするとデータベースが登録されません。私は私の初期化とデータベースのコピーに何か問題があることを知っている。データベースは私が推測するアプリケーションバンドルに保存されていません。私が間違っていることについての指導をしたいと思います。SQLiteデータベースがiPhoneにコピーされない

データベース内のデータも編集する予定ですので、これを考慮してください。ここで

コードされています

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

// Use this section to bring in database and populate the array 
dbPath = @"https://stackoverflow.com/users/jr/Documents/projectReference/reference.db"; 
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];  
[database open]; 

// Init the subjects Array 
categories = [[NSMutableArray alloc] init]; 
subjects = [[NSMutableArray alloc] init]; 
quotes = [[NSMutableArray alloc] init]; 
quoteMap = [[NSMutableArray alloc] init]; 

[self copyDatabaseIfNeeded]; 

// Open the database from the users filessytem 
NSLog(@"Going into the while loop for categories"); 
FMResultSet *result_categories = [database executeQuery:@"select distinct category from SUBJECT"]; 

}  

- (void) copyDatabaseIfNeeded { 

//Using NSFileManager we can perform many file system operations. 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSString *newdbPath = [self dbPath]; 
BOOL success = [fileManager fileExistsAtPath:newdbPath]; 

if(!success) { 

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"reference.db"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:newdbPath error:&error]; 
    NSLog(@"Database file copied from bundle to %@", newdbPath); 

    if (!success) 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
} else { 

    NSLog(@"Database file found at path %@", newdbPath); 

} 
} 

答えて

1

静的のdir @ "/ユーザ/ JR /ドキュメント/ projectReference/reference.db" データベースへは、iOSのコンテキストで間違っています。 相対パスの検出を使用する必要があります。あなたは、実行時にDBを作成し、文書に保管している場合、このいずれかを使用

NSString *path = [[NSBundle mainBundle] pathForResource:@"reference" ofType:@"db" inDirectory:@""]; 

:あなたは、バンドルであなたのDBを保存し、他のリソースとそれをコピーした場合、この1つだけを使用

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 
NSString *dbPath = [resourcePath stringByAppendingPathComponent:@"reference.db"] 

かこれについてはherehereとお読みください。

+0

ありがとう、これで解決しました。 – jroyce

関連する問題