【发布时间】:2016-04-30 16:35:39
【问题描述】:
我知道FMdb 并将其用于我的项目,但我不知道如何使用现有的数据库文件以及在哪里可以在我的项目中添加我的DB.sqlite?
【问题讨论】:
我知道FMdb 并将其用于我的项目,但我不知道如何使用现有的数据库文件以及在哪里可以在我的项目中添加我的DB.sqlite?
【问题讨论】:
在你的情况下:
将以下代码添加到您的 AppDelegate.swift 文件中
func checkDataBase(){
print("check database...")//log
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("DB.sqlite")
// Load the existing database
if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
print("Not found, copy one!!!")//log
let sourceSqliteURL = NSBundle.mainBundle().URLForResource("DB", withExtension: "sqlite")!
let destSqliteURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("DB.sqlite")
do {
try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURL, toURL: destSqliteURL)
} catch {
print(error)
}
}else{
print("DB file exist")//log
}}
3.Function 已经可以使用了,可以在“func application”中调用“checkDataBase()”函数..
【讨论】: