如何使用 xcode 4.3.2 在 iphone 中使用 sqlite 数据库...。我在查找数据库路径时遇到问题?因为我已经在我的应用程序的数据库文件夹中复制了 FormDB.sqlite ......请帮助我,因为我是 iphone 编程新手......
在 Appdelegate 文件的 applicationDidFinishLaunching 中调用此方法,但它返回 success=false
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"FormDB.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"FormDB.sqlite"];
}