解決Sqlite在IOS應用中使用方法案例實現

來源:互聯網
上載者:User

SqliteIOS應用中如何應用是本文要介紹的內容,主要是來瞭解IOS應用sqlite的使用方法,sqlite資料庫初始化,複製到使用者目錄,並判斷是否資料庫已經存在,或者複製是否成功!

在AppDelegate.m中輸入以下代碼,以便複製預置資料庫到指定doucment目錄

 
  1. - (BOOL) initializeDb {  
  2. NSLog (@"initializeDB");  
  3. // look to see if DB is in known location (~/Documents/$DATABASE_FILE_NAME)  
  4. //START:code.DatabaseShoppingList.findDocumentsDirectory  
  5.  
  6. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  7.  
  8. NSString *documentFolderPath = [searchPaths objectAtIndex: 0];  
  9. //查看檔案目錄  
  10. NSLog(@"%@",documentFolderPath);  
  11. dbFilePath = [documentFolderPath stringByAppendingPathComponent:@"shopping.db"];  
  12. //END:code.DatabaseShoppingList.findDocumentsDirectory  
  13. [dbFilePath retain];  
  14. //START:code.DatabaseShoppingList.copyDatabaseFileToDocuments  
  15. if (! [[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) {  
  16. // didn't find db, need to copy  
  17. NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:@"shopping" ofType:@"db"];  
  18. if (backupDbPath == nil) {  
  19. // couldn't find backup db to copy, bail  
  20. return NO;  
  21. } else {  
  22. BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:dbFilePath error:nil];  
  23. if (! copiedBackupDb) {  
  24. // copying backup db failed, bail  
  25. return NO;  
  26. }  
  27. }  
  28. }  
  29. return YES;  
  30. //END:code.DatabaseShoppingList.copyDatabaseFileToDocuments  
  31. NSLog (@"bottom of initializeDb");  
  32. }  
  33. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  34. // copy the database from the bundle if necessary  
  35. if (! [self initializeDb]) {  
  36. // TODO: alert the user!  
  37. NSLog (@"couldn't init db");  
  38. return;  
  39. }  
  40.     // Add the tab bar controller's current view as a subview of the window  
  41.     [window addSubview:tabBarController.view];  

小結:解決SqliteIOS應用中使用方法案例實現的內容介紹完了,希望通過本文能對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.