Copy data to the Application Data folder on iPhone

I am in the process of (finally) downloading my application on the iPhone for testing. So far I have tested the application only in the simulator. The application is data oriented and uses a SQLite database. I created the appropriate SQLite database with the data samples that I used in the simulator. How can I copy this .sqlite3 file to a real iPhone?

This was easy with the simulator, because I could just copy the .sqlite3 file to the ~ / Library / Application Support / iPhone Simulator / User / Applications / {UUID} / Documents folder, but I cannot figure out how to get it in the same place on the iPhone device itself. Playing the database from scratch on the phone is not an option, since I already did all the hard work, creating the database on a Mac.

+5
source share
3 answers

Why not add it to your application package? Right-click on the folder Resourcesin the project window in Xcode, then select Add> Existing Files...to add the sqlite database.

Then you download the file from the application package:

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"mySQLiteDatabaseFile" ofType:@"sqlite3"];
NSData *databaseData = [NSData dataWithContentsOfFile:databasePath];
// ...
+6
source

As Alex said, you should add your database file as a resource for your project. This will instruct Xcode to link your database file in your .app. However, this file is read-only, you need to copy it to the application documents folder (on the device or simulator, the same thing), where it will become available for writing mainly.

, . , , "" .app. 'pathLocal', () ...

YES, ( ). :)

NSString *pathLocal, *pathBundle;

// Automatically copy DB from .app bundle to device document folder if needed
- (BOOL)automaticallyCopyDatabase {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    pathLocal = [documentsDir stringByAppendingPathComponent:@"mydb.sqlite"];
    pathBundle = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSDictionary *localAttr = [fileManager fileAttributesAtPath:pathLocal traverseLink:YES];
    BOOL needsCopy = NO;
    if (localAttr == nil) {
        needsCopy = YES;
    } else {
        NSDate *localDate;
        NSDate *appDBDate;
        if (localDate = [localAttr objectForKey:NSFileModificationDate]) {
            NSDictionary *appDBAttr = [fileManager fileAttributesAtPath:pathBundle traverseLink:YES];
            appDBDate = [appDBAttr objectForKey:NSFileModificationDate];
            needsCopy = [appDBDate compare:localDate] == NSOrderedDescending;
        } else {
            needsCopy = YES;
        }
    }
    if (needsCopy) {
        NSError *error;
        BOOL success;
        if (localAttr != nil) {
            success = [fileManager removeItemAtPath:pathLocal error:&error];
        }
        success = [fileManager copyItemAtPath:pathBundle toPath:pathLocal error:&error];
        return success;
    }
    return YES;
}
+11

, , DBManagement m . @implementation

static sqlite3 * CHManagement = nil;

-(NSString *) getDBPath
{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *MyDatabasePath = [documentsDirectory stringByAppendingString:@"/CHManagement.sqlite"];
  return MyDatabasePath;

 }

-(void) checkDataBase
{
NSLog(@"CheckDatabase Called");
NSFileManager *fileManager=[NSFileManager defaultManager];
NSError *error=[[[NSError alloc]init] autorelease]; 
NSString *dbPath = [self getDBPath];
BOOL success=[fileManager fileExistsAtPath:dbPath];
if(!success)
{
 NSString *defaultDBPath=nil;
 defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"CHManagement.sqlite"];
 success=[fileManager copyItemAtPath:defaultDBPath  toPath:dbPath error:&error];
 if(!success)
 {
   NSAssert1(0,@"failed to create database with message '%@'.",[error localizedDescription]); 
  }
 }
 else
 {
 sqlite3 *MyDatabase;
 NSInteger VersionNumber=0;
 sqlite3_stmt *CompiledStatement=nil;
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *MyDatabasePath = [documentsDirectory stringByAppendingString:@"/CHManagement.sqlite"];
 if(sqlite3_open([MyDatabasePath UTF8String],&MyDatabase) == SQLITE_OK)
 {
  sqlite3_prepare_v2(MyDatabase, "select appVersionNo from VersionInfo", -1, &CompiledStatement, NULL);
  while(sqlite3_step(CompiledStatement) == SQLITE_ROW)
  {
   VersionNumber=sqlite3_column_int(CompiledStatement,0);
  }
  sqlite3_reset(CompiledStatement);
  sqlite3_finalize(CompiledStatement);
  sqlite3_close(MyDatabase);
 }
 if (VersionNumber!=1)
 {
  [self deleteDatabase];
  NSString *defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"CHManagement.sqlite"];
  success=[fileManager copyItemAtPath:defaultDBPath  toPath:dbPath error:&error];
  if(!success)
  {
   NSAssert1(0,@"failed to create database with message '%@'.",[error localizedDescription]); 
   }
  }
 }
 }

- (void)deleteDatabase
{
NSLog(@"Delete Database Called");
NSError **error=nil;
NSFileManager *FileManager = [NSFileManager defaultManager];
NSString *databasepath = [self getDBPath];
  BOOL success = [FileManager fileExistsAtPath:databasepath];
if(success) {
[FileManager removeItemAtPath:databasepath error:error];
}
 }

Using these methods, you can easily upload the database to the iphone application's document folder. Sorry for the formatting. and appVersionNo is a field in the tabular version of VersionInfo, the default value is 1

0
source

All Articles