Sqlite如何在IOS開發中應用是本文要介紹的內容,主要是來學習在IOS開發中sqlite數據庫的使用方法。sqlite數據庫初始化,複製到用戶目錄,並判斷是否數據庫已經存在,或者複製是否成功! sql
在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];
- }