swapnil patel
swapnil patel

Reputation: 394

How to import sqlite file into app's document directory in ios?

I want to export my whole database from my app via email and on other device when i download that database file, it can be open directly in app and replace my existing database file with that database file.

I have exported my database file(.sqlite) via mail and i want to import that file from mail into my app. I have also implement the functionality which the file from mail can directly open in my app. But i want to import that database file directly from mail. So how can i do that?

Or Can i replace that file with my app database?

Upvotes: 1

Views: 589

Answers (5)

Sargis
Sargis

Reputation: 1334

// Returns the persistent store coordinator for the application. // If the coordinator doesn't already exist, it is created and the application's store added to it.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil)
{
    return _persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"yourSqlite.sqlite"];

NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"yourSqlite.sqlite" ofType:nil];

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *error = nil;

if  (![[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"yourSqlite.sqlite"] ]) {

    if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:[documentsDirectory stringByAppendingPathComponent:@"yourSqlite.sqlite"] error:&error]){
        NSLog(@"Default file successfully copied over.");
    } else {
        NSLog(@"Error description-%@ \n", [error localizedDescription]);
        NSLog(@"Error reason-%@", [error localizedFailureReason]);
    }
}

_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _persistentStoreCoordinator;
}

Upvotes: 0

Vasanth
Vasanth

Reputation: 420

You should use iOS share extensions to first import the database file from mail app. Here is a tutorial that you can refer to.

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27428

I think you are looking for below method,

- (BOOL)replaceItemAtURL:(NSURL *)originalItemURL withItemAtURL:(NSURL *)newItemURL backupItemName:(nullable NSString *)backupItemName options:(NSFileManagerItemReplacementOptions)options resultingItemURL:(NSURL * _Nullable * _Nullable)resultingURL error:(NSError **)error NS_AVAILABLE(10_6, 4_0);

you can call this method with NSFileManager's instance or object. You can pass source and destination path as fileUrl ([NSURL fileURLWithPath:@"path string"];) and it will replace data at destination path!

Upvotes: 1

iOS dev
iOS dev

Reputation: 2284

This link is helps to all your questions in SQLITE,

Like Copy Sqlite File in to Docs directory,

and also Select queries, Insert, Delete, Update varius types of recotds etc.. etc...

Checkout + (NSString*) copyDBFile method in this link for ur requirement

SQLite Programing : Initial Steps - By iOSCodeGUIDE

//Commen method for DAtaBase Copy
+ (NSString*) copyDBFile
{
    NSString *docsDirectoryPath;
    NSArray *dirPaths;
    NSString *databasePath;
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDirectoryPath = [dirPaths objectAtIndex:0];
    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDirectoryPath stringByAppendingPathComponent: @"SqliteTable.db"]];

    BOOL isSuccess = [fileManager fileExistsAtPath: databasePath ];

    if (!isSuccess)
    {
        NSError *error;
        NSString *defaultDBPath=[[NSBundle mainBundle]pathForResource:@"SqliteTable" ofType:@"db"];
        // NSLog(@"path :%@", defaultDBPath);
        isSuccess = [fileManager copyItemAtPath:defaultDBPath toPath:databasePath error:&error];

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

    NSLog(@"%@",databasePath);

    return databasePath;

}

Upvotes: 0

Codobux
Codobux

Reputation: 1038

Couldn't understand your problem completely but here is a code that can give any clue how to replace your existing file in documents directory:

    //Reference the path to the documents directory.
    NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //Get the path of old file.
    NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"];

   if([[NSFileManager defaultManager] fileExistsAtPath: filePath])
   {
      [fileManager removeItemAtPath:filePath error:&error] //Delete old file
   }
   //Copy New File.sqlite from Resource Bundle.
   NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"sqlite"];
   [fileManager copyItemAtPath:resourcePath toPath:filePath error:&error]; 

Upvotes: 0

Related Questions