0
我知道FMdb
並將其用於我的項目,但我不知道如何使用現有的數據庫文件,以及在哪裏可以將我的DB.sqlite
添加到我的項目中?如何從swift項目的文件夾中使用現有的DB.sqlite?
我知道FMdb
並將其用於我的項目,但我不知道如何使用現有的數據庫文件,以及在哪裏可以將我的DB.sqlite
添加到我的項目中?如何從swift項目的文件夾中使用現有的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.函數可以使用,只需在「func應用程序」中調用「checkDataBase()」函數即可。
請參閱鏈接http://www.theappguruz.com/blog/use-sqlite-database-swift – kb920
http://stackoverflow.com/questions/17080018/use-and-access-existing-sqlite-database-on-ios –