2012-02-08 14 views
0

db.sqliteが見つからない理由がわかりません。私はそれをプロジェクトのリソースディレクトリに追加しました。それはそこにある。 ここにいくつかのコードがあります。 // EDIT データベースファイルが見つかりません

-(void)getDatabaseLocation 
{ 
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *docsDir = [dirPaths objectAtIndex:0]; 

    // Build the path to the database file 
    NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"db.sqlite"]]; 

    NSFileManager *filemgr = [NSFileManager defaultManager]; 
    if ([filemgr fileExistsAtPath: databasePath ] == NO) 
    { 
     NSLog(@"NO"); // It's say no when this method is called. 
    } 
    else 
    { NSLog(@"YES"); 
    } 
} 

今、私はdocsDirでパスが存在しないことを確認してください。

答えて

2

もちろんありません。

if (![filemgr fileExistsAtPath:databasePath]) 
{ 
    [filemgr copyItemAtPath:[NSBundle.mainBundle pathForResource:@"db" ofType:@"sqlite"] toPath:databasePath error:nil]; 
} 

EDIT:あなたは、まずこのようなドキュメントディレクトリにリソースをコピーする必要があります

databasePath = [NSBundle.mainBundle pathForResource:@"db" ofType:@"sqlite"]; 
:あなたが唯一のデータベース(無編集)で検索を実行する必要がある場合は、単にこれを行うことができます
+0

彼はそれを変更したい場合、docdirにコピーする必要があります。ルックアップの場合にのみ、リソースディレクトリにそのまま残すことができます。 –

+0

私はSELECT文のためにデータベースが必要です – Profo

+0

@Profo書き込み可能なデータベースが必要ない場合、私の答えを編集しました。 –

関連する問題