How to Apply Sqlite in IOS development

Source: Internet
Author: User

SqliteHowIOS developmentApplication is the content to be introduced in this article, mainly to learnIOS developmentMediumSqlite Database.Sqlite DatabaseInitialize, copy to the user directory, and determine whether or notDatabaseAlready exists, or is the replication successful!

Enter the following code in AppDelegate. m to copy the preset database to the specified doucment directory.

 
 
  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. NSArray * searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  6. NSString * documentFolderPath = [searchPaths objectAtIndex: 0];
  7. // View the file directory
  8. NSLog (@ "% @", documentFolderPath );
  9. DbFilePath = [documentFolderPath stringByAppendingPathComponent: @ "shopping. db"];
  10. // END: code. DatabaseShoppingList. findDocumentsDirectory
  11. [DbFilePath retain];
  12. // START: code. DatabaseShoppingList. copyDatabaseFileToDocuments
  13. If (! [[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) {
  14. // Didn't find db, need to copy
  15. NSString * backupDbPath = [[NSBundle mainBundle] pathForResource: @ "shopping" ofType: @ "db"];
  16. If (backupDbPath = nil ){
  17. // Couldn't find backup db to copy, bail
  18. Return NO;
  19. } Else {
  20. BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath: backupDbPath toPath: dbFilePath error: nil];
  21. If (! CopiedBackupDb ){
  22. // Copying backup db failed, bail
  23. Return NO;
  24. }
  25. }
  26. }
  27. Return YES;
  28. // END: code. DatabaseShoppingList. copyDatabaseFileToDocuments
  29. NSLog (@ "bottom of initializeDb ");
  30. }
  31. -(Void) applicationDidFinishLaunching :( UIApplication *) application {
  32. // Copy the database from the bundle if necessary
  33. If (! [Self initializeDb]) {
  34. // TODO: alert the user!
  35. NSLog (@ "couldn't init db ");
  36. Return;
  37. }
  38. // Add the tab bar controller's current view as a subview of the window
  39. [Window addSubview: tabBarController. view];
  40. }

Summary: DetailsSqliteHowIOS developmentI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.