iPhone模拟器在哪里放数据文件?

时间:2012-07-05 09:32:32

标签: iphone xcode

如果我使用已构建到捆绑包中的数据库编译我的iPhone应用程序,我必须使用哪条路径来查找文件,以便我可以打开它,如:

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

2 个答案:

答案 0 :(得分:0)

无论模拟器或设备如何,您都可以这样使用。请不要忘记将db文件添加到项目文件夹中并添加到您的软件包中。

#define DATABASE @"databasefile.sqlite"


NSArray *documentPaths      = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir      = [documentPaths objectAtIndex:0];
databasePath                = [[NSString alloc] initWithString:[documentsDir stringByAppendingPathComponent:DATABASE]];

答案 1 :(得分: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];

}