我的应用程序使用了 sqllite 数据库,我需要在文档文件夹中复制数据库。但是当检查是否已经有数据库文件时,它已经存在,并且不会复制。在文档文件夹中启动应用程序,创建一个空文件。
import Foundation
import UIKit
import SQLite
public class myUITabBarController: UITabBarController, NSURLConnectionDelegate, NSXMLParserDelegate {
var db = Database("\(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String)/Base.db")
.......
func pathToDocsFolder() -> String {
let pathToDocumentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
return pathToDocumentsFolder.stringByAppendingPathComponent("/Base.db")
}
public override func viewDidLoad() {
super.viewDidLoad()
let theFileManager = NSFileManager.defaultManager()
if theFileManager.fileExistsAtPath(pathToDocsFolder()) {
println("File Found!")
// And then open the DB File
}
else {
// Copy the file from the Bundle and write it to the Device:
let pathToBundledDB = NSBundle.mainBundle().pathForResource("Base", ofType: "db")
let pathToDevice = pathToDocsFolder()
let error:NSError?
// Here is where I get the error:
if (theFileManager.copyItemAtPath(pathToBundledDB!, toPath:pathToDevice, error:nil)) {
// success
}
else {
// failure
}
}
var mPath = pathToDocsFolder()
db = Database(mPath)
最佳答案
试试这个:从 bundle 复制 base
关于ios - 为什么要创建一个空数据库(0 口)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206023/