How to Implement Sqlite in IOS applications

Source: Internet
Author: User

SqliteInIOS appIn this article, we will introduce how to applyIOS appMediumSqlite,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 presetDatabaseTo 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.  
  6. NSArray * searchPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  7.  
  8. NSString * documentFolderPath = [searchPaths objectAtIndex: 0];
  9. // View the file directory
  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];
  42. }

Conclusion: SolutionSqliteInIOS appThis article is helpful to you!

Related Article

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.