【问题标题】:Copy Sqlite DataBase in Swift does not work properly在 Swift 中复制 Sqlite 数据库无法正常工作
【发布时间】:2017-01-10 07:50:47
【问题描述】:

我正在为目标 c 使用以下代码来复制 sqlite 数据库,它工作正常。但是当我将此代码转换为 swift 时,它会在 Bool 类型上显示错误。

这是目标c代码

  - (void) copyDatabaseIfNeeded {

  //Using NSFileManager we can perform many file system operations.
   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSError *error;

   NSString *dbPath = [self getDBPath];
   BOOL success = [fileManager fileExistsAtPath:dbPath];

  if(!success) {

   NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
   success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

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

  - (NSString *) getDBPath
  {   

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
   NSString *documentsDir = [paths objectAtIndex:0];
   return [documentsDir   stringByAppendingPathComponent:@"database.sqlite"];
  }

这是导致问题的 Swift 的 CopyDataBase。

   var fileManager = FileManager.default
   var error: Error!
   var dbPath = self.getDBPath()
   var success = fileManager.fileExists(atPath: dbPath)
   if !success {
   var defaultDBPath = URL(fileURLWithPath: Bundle.main.resourcePath!).appendingPathComponent("CapalinoDataBase.sqlite").absoluteString
   do {
    success = try fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath)
    }
    catch {
    }
    if !success {
    assert(false, "Failed to create writable database file with message '\(error.localizedDescription)'.")
    }
      }

【问题讨论】:

    标签: ios objective-c sqlite swift3


    【解决方案1】:

    请试试这个。

    func copyDatabse() {
    
            let fileMgr = FileManager.default
    
            if let path = Bundle.main.path(forResource: "db", ofType:"sqlite") {
    
                do {
                    try fileMgr.copyItem(atPath: path, toPath: dbPath())
                    print("Copy success")
                }
                catch {
                    print(error.localizedDescription )
                }
            }
        }
    
    
        func dbPath() -> String {
    
            let dirPaths =  NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)
            let docsDir = dirPaths[0]
    
            let destPath = (docsDir as NSString).appendingPathComponent("/db.sqlite")
    
            return destPath
        }
    

    【讨论】:

      【解决方案2】:

      在 swift 中使用单吨类使用 SQLIte 的最佳方式。

      Download example

      func methodToCreateDatabase() -> NSURL? {
      
          let fileManager = NSFileManager.defaultManager()
      
          let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
      
          if let documentDirectory:NSURL = urls.first { // No use of as? NSURL because let urls returns array of NSURL
      
              // exclude cloud backup
              do {
                  try documentDirectory.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
              } catch _{
                  print("Failed to exclude backup")
              }
      
              // This is where the database should be in the documents directory
              let finalDatabaseURL = documentDirectory.URLByAppendingPathComponent("contact.db")
      
              if finalDatabaseURL.checkResourceIsReachableAndReturnError(nil) {
                  // The file already exists, so just return the URL
                  return finalDatabaseURL
              } else {
                  // Copy the initial file from the application bundle to the documents directory
                  if let bundleURL = NSBundle.mainBundle().URLForResource("contact", withExtension: "db") {
      
                      do {
                          try fileManager.copyItemAtURL(bundleURL, toURL: finalDatabaseURL)
                      } catch _ {
                          print("Couldn't copy file to final location!")
                      }
      
                  } else {
                      print("Couldn't find initial database in the bundle!")
                  }
              }
          } else {
              print("Couldn't get documents directory!")
          }
      
          return nil
      }
      

      【讨论】:

        【解决方案3】:
        Please try this one it is working on swift 3.0
        
        func copyDatabaseIfNeeded() {
            //Using NSFileManager we can perform many file system operations.
            let fileManager = FileManager.default
            let error: Error?
            let dbPath: String = self.getDBPath()
            var success: Bool = fileManager.fileExists(atPath: dbPath)
            if !success {
                let defaultDBPath: String = URL(fileURLWithPath: (Bundle.main.resourcePath)!).appendingPathComponent("database.sqlite").absoluteString
        
                do {
                    success = try fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath) as Any as! Bool
                }
                catch let error as NSError {
                    print("Ooops! Something went wrong: \(error)")
                }
        
                if !success {
                    assert(false, "Failed to create writable database file with message '\(error?.localizedDescription)'.")
                }
            }
        }
        
        func getDBPath() -> String {
            let paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
            let documentsDir: String = paths[0] as! String
            return URL(fileURLWithPath: documentsDir).appendingPathComponent("database.sqlite").absoluteString
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-13
          相关资源
          最近更新 更多