【问题标题】:How to import sqlite file into app's document directory in ios?如何在ios中将sqlite文件导入应用程序的文档目录?
【发布时间】:2017-05-17 11:58:05
【问题描述】:

当我下载该数据库文件时,我想通过电子邮件和其他设备从我的应用程序导出我的整个数据库,它可以直接在应用程序中打开并用该数据库文件替换我现有的数据库文件。

我已经通过邮件导出了我的数据库文件(.sqlite),我想将该文件从邮件导入到我的应用程序中。我还实现了邮件文件可以直接在我的应用程序中打开的功能。但我想直接从邮件中导入该数据库文件。那么我该怎么做呢?

或者我可以用我的应用程序数据库替换那个文件吗?

【问题讨论】:

    标签: ios objective-c sqlite import export


    【解决方案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"];) 传递,它将替换目标路径中的数据!

    【讨论】:

    • 但是我必须在 Url 中写什么来指定从邮件下载的文件的路径?
    • 如果您正在下载文件,这意味着它将存储在某个地方,所以找出它!或者向您的代码展示您是如何获取该文件的!
    • 我没有实现任何东西来将该文件导入我的应用程序。我也想知道如何从邮件中获取该文件到我的应用程序中。
    • 当我从邮件下载文件时,我已经实现了该文件可以在我的应用程序中打开。但该文件未安装在我的应用程序的文档目录中
    【解决方案2】:

    无法完全理解您的问题,但这里有一段代码可以提供任何线索来替换文档目录中的现有文件:

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

    【讨论】:

    • 我不想用捆绑包中的文件替换数据库文件。它只会删除现有数据库并将新数据库文件复制到文档目录中。我想将我的电子邮件中的数据库文件导入我的应用程序。
    【解决方案3】:

    此链接对您在 SQLITE 中的所有问题都有帮助,

    点赞复制Sqlite文件到Docs目录

    还可以选择查询、插入、删除、更新各种类型的记录等......

    在此链接中检查 + (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;
    
    }
    

    【讨论】:

    • ....没有。如果我从我的包中替换我的数据库文件,那么它只会复制空白数据库。我想从已经包含一些数据的邮件中导入数据库文件。
    【解决方案4】:

    您应该使用 iOS 共享扩展首先从邮件应用程序导入数据库文件。这是您可以参考的tutorial

    【讨论】:

      【解决方案5】:

      // 返回应用程序的持久存储协调器。 // 如果协调器尚不存在,则创建它并将应用程序的存储添加到其中。

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

      【讨论】:

        猜你喜欢
        • 2013-10-31
        • 2013-03-01
        • 2013-05-14
        • 2015-11-25
        • 2018-04-13
        • 2017-04-28
        • 1970-01-01
        • 2013-08-08
        • 2021-02-27
        相关资源
        最近更新 更多