2011-06-01 194 views
0

任何人都可以幫我嗎在iPhone中創建數據庫副本

我們如何創建數據庫副本。

我們該如何在iPhone中打開該數據庫。

我們如何才能在我們的應用程序路徑

創建文件夾,我們如何可以複製在我們的應用程序路徑

thanx提前文件的文件夾。

回答

5

「用於創建數據庫的副本......」

BOOL success; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentPath = [paths objectAtIndex:0]; 
NSString *finalPath = [documentPath stringByAppendingPathComponent:@"test.sqlite"]; 

success = [fileManager fileExistsAtPath:finalPath]; 

if(success) 
{ 
    NSLog(@"Database Already Created."); 
    return; 
} 

NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.sqlite"]; 

success = [fileManager copyItemAtPath:defaultPath toPath:finalPath error:&error]; 

if(success) 
{ 
    NSLog(@"Database Created Successfully."); 
} 

「對於打開該數據庫...」

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentPath = [paths objectAtIndex:0]; 
NSString *finalPath = [documentPath stringByAppendingPathComponent:@"test.sqlite"]; 

if(sqlite3_open([finalPath UTF8String], &database) != SQLITE_OK) 
{ 
    sqlite3_close(database); 
    NSLog(@"Error to Open Database :- %s",sqlite3_errmsg(database)); 
} 

「對於應用程序路徑創建文件夾「

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 

NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/FolderName"]; 

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
{ 
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 
} 

「要複製的文件和文件夾的應用」

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 

NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/FolderName"]; 

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
{ 
    BOOL successs; 

    NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Files/FolderName"]; 

    successs = [[NSFileManager defaultManager] fileExistsAtPath:defaultPath]; 

    if(successs) 
    { 
     NSLog(@"TRUE"); 
     NSString *strFile = [NSString stringWithFormat:@"%@",dataPath]; 

     NSLog(@"File :- '%@'",strFile); 

     [fileManager copyItemAtPath:defaultPath toPath:strFile error:&error]; 
    } 
    else 
     NSLog(@"FALSE"); 
} 
相關問題