我在我的項目稱爲mydb.sqlite一個數據庫,我用:別找我的數據庫資源
databasePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
得到的路徑和IR回報databasePath --> null
我在「複製包資源」檢查和它出現在這裏。我想我錯過了什麼。
我在我的項目稱爲mydb.sqlite一個數據庫,我用:別找我的數據庫資源
databasePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
得到的路徑和IR回報databasePath --> null
我在「複製包資源」檢查和它出現在這裏。我想我錯過了什麼。
問題是我在一個NSObject中調用了 [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"];
。這就是爲什麼它可以找到數據庫。現在我把它叫做ControlView
-(void)checkAndCreateDatabase
{
//Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
//Create a FileManager object, we will use this to check the status
//of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
//Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:[self databasePath]];
NSLog(@"databasePath = %@",databasePath);
NSLog(@"Success = %d",success);
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databaseName error:nil];
}
-(NSString *)databasePath
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"YouDatabaseName.sqlite"]];
return databasePath;
}
調用此檢查,並在的applicationDidFinishLaunching在AppDelegate中
創建功能希望它會幫助你。
我試過它不起作用:/ – 2013-05-07 11:25:12
您是否創建了檢查和創建方法,用於檢查數據庫是否存在於文檔direntory中,如果數據庫不存在於它將該數據庫文件複製到文檔的文檔目錄中目錄 。 – 2013-05-07 11:29:34
檢查我更新的答案。 – 2013-05-07 11:38:21
試試這個NSURL * path = [NSURL fileURLWithPath:[NSHomeDirectory()stringByAppendingPathComponent:@「Documents/mydb.sqlite」]]; – Buntylm 2013-05-07 11:32:05