开发者

Create copy of database in iPhone

开发者_开发技巧Can any one help me for:

  • How can we create copy of database?

  • How can we open that database in iPhone?

  • How can we create folder in our application path?

  • How can we copy folder with files in our application path?


"For Creating the copy of Database..."

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.");
}

"For Open that Database..."

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

"For Creating Folder in Application Path"

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

"For Copying The Files And Folder in Application"

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");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜