2013-03-19 17 views
14

我想每次啓動應用程序時,將最新更新的數據庫位置的sqlite數據庫複製到iOS應用程序。如何在iOS中啓動應用程序時複製sqlite數據庫?

有沒有辦法做到這一點?

+0

請,請確保這樣的問題,你做你嘗試過什麼來源或只是在你的代碼寫在這裏列表的引用,這樣每個人都可以知道尤爾方法/ – madLokesh 2013-07-24 11:59:07

回答

16

您可以添加下列方法將的appdelegate

- (void) copyDatabaseIfNeeded { 

    //Using NSFileManager we can perform many file system operations. 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 

    NSString *dbPath = [self getDBPath]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) { 

     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

     if (!success) 
      NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

- (NSString *) getDBPath 
{ 
    //Search for standard documents using NSSearchPathForDirectoriesInDomains 
    //First Param = Searching the documents directory 
    //Second Param = Searching the Users directory and not the System 
    //Expand any tildes and identify home directories. 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    //NSLog(@"dbpath : %@",documentsDir); 
    return [documentsDir stringByAppendingPathComponent:@"database.sqlite"]; 
} 

,並啓動方法 [self copyDatabaseIfNeeded];希望這將有助於調用此方法在你沒有完成。

+0

感謝ü... 它的工作原理 – 2013-03-19 04:26:31

+0

不,如果您的原始數據庫很髒,您希望保留用戶活動數據而不是覆蓋整個數據庫。 – OMGPOP 2013-06-06 14:58:52

+0

新更換的數據庫是否可寫?我已經替換了一個數據庫,但它變成了只讀。我怎樣才能使它可寫? – 2017-03-14 08:00:20

0

試試這個:

NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *strDocDirectory = [docPath objectAtIndex:0]; 

    self.strDatabasePath = [strDocDirectory stringByAppendingPathComponent:YOUR_DB]; 


    NSFileManager *filemgr = [NSFileManager defaultManager]; 

    if ([filemgr fileExistsAtPath: self.strDatabasePath ] == NO) 
    { 
     const char *dbpath = [self.strDatabasePath UTF8String]; 
     sqlite3 *contactDB; 


     if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
     { 
      char *errMsg; 
      const char *sql_stmt = "CREATE TABLE IF NOT EXISTS YOUR_Table (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"; 

      if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) 
      { 
       NSLog(@"Failed to create table"); 
      } 

      sqlite3_close(contactDB); 
0

AppDelegate類

試試這個.h文件中

@property(nonatomic, retain) NSString *dbPath; 

.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [self copyDatabaseIfNeeded]; 
} 
- (void) copyDatabaseIfNeeded 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    dbPath = [self getDBPath]; 

    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) 
    { 
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sampleDB.sqlite3"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 
     if (!success) 
      NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

/********* Database Path *********/ 
- (NSString *) getDBPath 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    return [documentsDir stringByAppendingPathComponent:@"sampleDB.sqlite3"]; 
} 

It'l l自動複製,如果在應用程序中沒有找到任何數據庫。

希望它能幫助你。

謝謝。

+0

試試這個,它一定會幫助你。 – 2013-03-19 04:08:03

+0

thnks ..其工作很好.... – 2013-03-19 04:26:01

+0

好的。好..享受編碼.. :) – 2013-03-19 04:26:23

4

下面的代碼使用了應對數據庫應用程序啓動時

在appdelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 

    [self getdatabase]; 

    return YES; 
} 

,並在您appdelegate.m下面添加功能

-(void)getdatabase 
{ 
    BOOL success; 
    NSFileManager *filemanager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *DBPath = [pathArray objectAtIndex:0]; 
    NSString *writableDBPath = @""; 
    writableDBPath = [DBPath stringByAppendingPathComponent:@"xyz.sqlite"]; 
    NSLog(@"writableDBPath:%@",writableDBPath); 
    success = [filemanager fileExistsAtPath:writableDBPath]; 
    if (!success) { 
     NSString *defaultDBpath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"xyz.sqlite"]; 
     success = [filemanager copyItemAtPath:defaultDBpath toPath:writableDBPath error:&error]; 
     if (!success) { 
      NSAssert(0, @"failed to copy database at path with message '%@'.",[error localizedDescription]); 
     } 
    }  
    NSLog(@"111writableDBPath:%@",writableDBPath);  
} 
+0

謝謝...它對我很好! – 2013-03-19 04:16:04

0
-(void)openDatase{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    self.databasePath = [documentsDirectory stringByAppendingPathComponent:@"weightCal.sqlite"]; 
    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     NSLog(@"Database Successfully Opened "); 
    } else { 
     NSLog(@"Error in opening database "); 
    } 
} 

-(void)dealloc{ 
    sqlite3_close(database); 
    [super dealloc]; 
} 


-(void)copyDatabase { 
    BOOL success; 
    NSFileManager *fileManager=[NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory=[paths objectAtIndex:0]; 
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"test.sqlite"]; 
    success = [fileManager fileExistsAtPath:writablePath]; 
    if(success) { 
     return; 
    } 
    NSString *defaultPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.sqlite"]; 
    success=[fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error]; 
    if(!success){ 
     NSAssert1(0,@"Failed to create writable database file with message '%@' .",[error localizedDescription]); 
    } 
} 

聲明TEO變量按照.h

sqlite3 *database; 
NSString *databasePath; 
相關問題