CommonDBManager.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // CommonDBManager.swift
  3. // dcwj
  4. //
  5. // Created by Virgil on 16/8/30.
  6. // Copyright © 2016年 Virgil. All rights reserved.
  7. //
  8. import UIKit
  9. let DATABASE_DB_NAME = "CommonFrame_2"
  10. let DATABASE_DB_EXT = "db"
  11. let DATABASE_DB_ALL_NAME = "\(DATABASE_DB_NAME).\(DATABASE_DB_EXT)"
  12. class CommonDBManager: NSObject {
  13. /// 获取数据库地址
  14. class func getDBPath() -> String {
  15. if CommonValue.getDBPath() != "" {
  16. return CommonUntils.getAppPath() + CommonValue.getDBPath()
  17. } else {
  18. return NSHomeDirectory() + "/Documents/" + DATABASE_DB_ALL_NAME
  19. }
  20. }
  21. class func initDB() {
  22. //获取应用程序的路径
  23. let searchPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  24. let documentFolderPath = searchPaths[0]
  25. let dbFilePath = documentFolderPath + "/"+DATABASE_DB_ALL_NAME
  26. print("==DBPath:\(dbFilePath)")
  27. let fm = FileManager.default
  28. let isExist = fm.fileExists(atPath: dbFilePath)
  29. /**
  30. * 如果不存在 拷贝工程里的数据库到 documents下
  31. */
  32. if !isExist {
  33. let backupDbPath = Bundle.main.path(forResource: DATABASE_DB_NAME, ofType: DATABASE_DB_EXT)
  34. do {
  35. _ = try fm.copyItem(atPath: backupDbPath!, toPath: dbFilePath)
  36. } catch {
  37. }
  38. } else {
  39. print(getDBPath())
  40. //存在则判断版本更新数据库
  41. }
  42. }
  43. }