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
- -(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];
- // View the file directory
- 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];
- }
Conclusion: SolutionSqliteInIOS appThis article is helpful to you!