0

バンドルに組み込まれているデータベースを使用してiPhoneアプリをコンパイルする場合、次のように、ファイルを開くためにファイルを見つけるためにどのパスを使用する必要がありますか。

FMDatabase* db = [FMDatabase databaseWithPath:@"/???/database.sqlite"]; 
4

2 に答える 2

0

シミュレーターやデバイスに関係なく、このように使用できます。プロジェクトフォルダにdbファイルを追加し、バンドルに追加することを忘れないでください。

#define DATABASE @"databasefile.sqlite"


NSArray *documentPaths      = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir      = [documentPaths objectAtIndex:0];
databasePath                = [[NSString alloc] initWithString:[documentsDir stringByAppendingPathComponent:DATABASE]];
于 2012-07-05T09:36:30.843 に答える
0

あなたの中に - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

次のコードを設定します。

databaseName=@"yourfileName.sqlite";    
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    [self checkAndCreateDatabase];

-(void)checkAndCreateDatabase {

BOOL success;


NSFileManager *fileManager = [NSFileManager defaultManager];

success = [fileManager fileExistsAtPath:databasePath];

if(success) return;
else
    printf("NO File found");

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

}

于 2012-07-05T09:40:44.107 に答える