我想通過電子郵件和其他設備從我的應用程序導出整個數據庫,當我下載該數據庫文件時,它可以直接在應用程序中打開,並用該數據庫文件替換現有的數據庫文件。如何將ios中的sqlite文件導入到應用程序的文檔目錄中?
我已經通過郵件導出了我的數據庫文件(.sqlite),我想從郵件導入該文件到我的應用程序。我還實現了郵件中的文件可以直接在我的應用程序中打開的功能。但我想直接從郵件中導入該數據庫文件。那我該怎麼做?
或者我可以用我的應用程序數據庫替換該文件嗎?
我想通過電子郵件和其他設備從我的應用程序導出整個數據庫,當我下載該數據庫文件時,它可以直接在應用程序中打開,並用該數據庫文件替換現有的數據庫文件。如何將ios中的sqlite文件導入到應用程序的文檔目錄中?
我已經通過郵件導出了我的數據庫文件(.sqlite),我想從郵件導入該文件到我的應用程序。我還實現了郵件中的文件可以直接在我的應用程序中打開的功能。但我想直接從郵件中導入該數據庫文件。那我該怎麼做?
或者我可以用我的應用程序數據庫替換該文件嗎?
我認爲你正在尋找下面的方法,
- (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"];)
,它將替換目標路徑上的數據!
這個鏈接可以幫助所有的問題在SQLITE,
像複製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;
}
....不。如果我從我的包中替換我的數據庫文件,那麼它將只複製空白數據庫。我想從包含已有數據的郵件導入數據庫文件。 –
無法完全,但這裏瞭解你的問題是,可以給任何線索如何在文件目錄替換現有文件中的代碼:
//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];
我不想用包中的文件替換數據庫文件。它只會刪除現有的數據庫並將新的數據庫文件複製到文檔目錄中。我想從我的電子郵件導入數據庫文件到我的應用程序。 –
您應該使用iOS共享擴展名首先從郵件應用程序導入數據庫文件。這裏是你可以參考的tutorial。
//返回應用程序的持久存儲協調器。 //如果協調器尚不存在,則創建它並將應用程序的存儲添加到其中。
- (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;
}
但是我必須寫在Url中才能指定從郵件中下載的文件的路徑? –
如果你正在下載文件,這意味着它將存儲在某個地方,所以找出它!或者顯示你的代碼,如何獲取該文件! – Lion
我還沒有實現任何將該文件導入到我的應用程序中的任何內容。我也想知道,我怎樣才能從郵件中獲取該文件到我的應用程序。 –