Sqlite如何在IOS开发中应用是本文要介绍的内容,主要是来学习在IOS开发中sqlite数据库的使用方法。sqlite数据库初始化,复制到用户目录,并判断是否数据库已经存在,或者复制是否成功!
在AppDelegate.m中输入以下代码,以便复制预置数据库到指定doucment目录
- (BOOL) initializeDb { NSLog (@"initializeDB"); // look to see if DB is in known location (~/Documents/$DATABASE_FILE_NAME) //START:code.DatabaseShoppingList.findDocumentsDirectory NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentFolderPath = [searchPaths objectAtIndex: 0]; //查看文件目录 NSLog(@"%@",documentFolderPath); dbFilePath = [documentFolderPath stringByAppendingPathComponent:@"shopping.db"]; //END:code.DatabaseShoppingList.findDocumentsDirectory [dbFilePath retain]; //START:code.DatabaseShoppingList.copyDatabaseFileToDocuments if (! [[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) { // didn't find db, need to copy NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:@"shopping" ofType:@"db"]; if (backupDbPath == nil) { // couldn't find backup db to copy, bail return NO; } else { BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:dbFilePath error:nil]; if (! copiedBackupDb) { // copying backup db failed, bail return NO; } } } return YES; //END:code.DatabaseShoppingList.copyDatabaseFileToDocuments NSLog (@"bottom of initializeDb"); } - (void)applicationDidFinishLaunching:(UIApplication *)application { // copy the database from the bundle if necessary if (! [self initializeDb]) { // TODO: alert the user! NSLog (@"couldn't init db"); return; } // Add the tab bar controller's current view as a subview of the window [window addSubview:tabBarController.view]; }