Copying data to the Application Data folder on the iPhone

后端 未结 3 1924
Happy的楠姐
Happy的楠姐 2020-12-29 17:29

I am in the process of (finally) loading my App onto an iPhone device for testing. So far I have only tested the App in the simulator. The application is data-centric and us

3条回答
  •  误落风尘
    2020-12-29 18:05

    here how i done this create a class called DBManagement and in m file implement these methods.also put this line in implementation file above @implementation line

    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];
    }
     }
    

    with these methods you can easily load database into iphone document folder of the app. Sorry about the formatting. and appVersionNo is a field in a tableCalled versionInfo the default value is 1

提交回复
热议问题