2017-05-17 15 views
0

自分のアプリケーションを電子メールやその他のデバイス経由でエクスポートしたいとき、そのデータベースファイルをダウンロードすると、アプリで直接開いて既存のデータベースファイルをそのデータベースファイルに置き換えることができます。iosのアプリケーションのドキュメントディレクトリにsqliteファイルをインポートするには?

データベースファイル(.sqlite)をメールでエクスポートしました。そのファイルをメールからアプリケーションにインポートします。私はまた、メールからのファイルが直接私のアプリで開くことができる機能を実装しています。しかし、私はそのデータベースファイルをメールから直接インポートしたい。だから私はどうすればいいの?

または、そのファイルをアプリケーションデータベースに置き換えることはできますか?

答えて

1

私はあなたが探していると思います

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

NSFileManagerのインスタンスまたはオブジェクトでこのメソッドを呼び出すことができます。ソースと目的地のパスをfileUrl ([NSURL fileURLWithPath:@"path string"];)として渡すことができ、目的地パスのデータを置き換えます!

+0

しかし、私はメールからダウンロードしたファイルのパスを指定するためにURLに書く必要がありますか? –

+0

どこかに保存されることを意味するファイルをダウンロードしている場合は、見つけてください!または、そのファイルをどのように入手したかをコードで確認してください! – Lion

+0

私は、そのファイルを自分のアプリケーションにインポートするためのものを実装していません。また、私はどのように私のアプリにメールからそのファイルを取得することができます知ってほしい。 –

0

このリンクされては、SQLiteの中のすべてのご質問に役立ちますDocsディレクトリにでコピーSqliteをファイルのように

ともクエリを選択し、挿入、削除などrecotdsの更新バリウス種類ウル要件

のため、このリンクインチなど...

アウト+ (NSString*) copyDBFile方法

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; 

} 
+0

....いいえ。私がバンドルから自分のデータベースファイルを置き換えると、空のデータベースだけがコピーされます。私は既にいくつかのデータを含むメールからデータベースファイルをインポートしたい。 –

0

は完全にあなたの問題を理解しますが、ここでのドキュメントディレクトリ内の既存のファイルを置き換えるために、どのように任意の手掛かりを与えることができ、コードではありませんでした:

//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]; 
+0

データベースファイルをバンドルのファイルに置き換えたくありません。既存のデータベースのみを消去し、新しいデータベースファイルをドキュメントディレクトリにコピーします。私は電子メールからアプリケーションにデータベースファイルをインポートしたい。 –

0

メールアプリケーションからデータベースファイルを最初にインポートするには、iOS共有拡張を使用する必要があります。あなたが参照できるtutorialがここにあります。

0

//アプリケーションの永続ストアコーディネータを返します。 //コーディネータが存在しない場合は、コーディネータが作成され、アプリケーションのストアが追加されます。

- (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; 
} 
関連する問題