无法在swift编程中找到外部sqlite文件

时间:2016-07-03 16:55:19

标签: ios swift sqlite

我是iOS编程新手。我试图用我的新iOS编程附加一个sqlite数据库记录。

我已经通过CopyBundle资源添加了我的sqlite文件 但通过编程我无法找到我的文件,它总是失败。请帮助

文件名包含值' KEERTHANAIGAL.sqlite"从AppDelegate文件传递

以下是我的编程

class Util: NSObject {
class func getPath(fileName: String) -> String {

   let filemanager = NSFileManager.defaultManager()

    let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
   let destinationPath = documentDirectoryPath.stringByAppendingPathComponent(fileName)
   let sourcePath = NSBundle.mainBundle().pathForResource("KEERTHANAIGAL", ofType: "sqlite")

    do {
    try filemanager.copyItemAtPath(sourcePath!, toPath: destinationPath)
    }
    catch let error1 as NSError{ print (" OOPS Something went wrong : \(error1)")}

    return destinationPath
}

class func copyFile(fileName: NSString) {
    let dbPath: String = getPath(fileName as String)
    let fileManager = NSFileManager.defaultManager()
    if !fileManager.fileExistsAtPath(dbPath) {

        let documentsURL = NSBundle.mainBundle().resourceURL
        let fromPath = documentsURL!.URLByAppendingPathComponent(fileName as String)

        var error : NSError?
        do {
            try fileManager.copyItemAtPath(fromPath.path!, toPath: dbPath)
        } catch let error1 as NSError {
            error = error1
        }
        let alert: UIAlertView = UIAlertView()
        if (error != nil) {
            alert.title = "Error Occured"
            alert.message = error?.localizedDescription
        } else {
            alert.title = "Successfully Copy"
            alert.message = "Your database copy successfully"
        }
        alert.delegate = nil
        alert.addButtonWithTitle("Ok")
        alert.show()
    }
}

以下代码尝试在路径

中读取数据库
@IBAction func getSong(sender: AnyObject) {
    let KeerthanaigalDB = FMDatabase(path: databasePath as String)
    if KeerthanaigalDB.open()
    {
        let querySQL = "SELECT NEXT SONGNAME, SONGLYRICS FROM KEERTHANAIGAL.DB"
        let results:FMResultSet? = KeerthanaigalDB.executeQuery(querySQL, withArgumentsInArray: nil)
        if results?.next() == true
        {
            songTitle.text = results?.stringForColumn("SONGNAME")
            songLyrics.text = results?.stringForColumn("SONGLYRICS")
            status.text = "Record Found"

        }
        else{
            status.text = "Record Not Found"
            songTitle.text = " "
            songLyrics.text = " "

        }
        KeerthanaigalDB.close()
    }
    else
    {
        print("Error: \(KeerthanaigalDB.lastErrorMessage())")
    }

代码总是转到'记录未找到'子句和调试器说"没有这样的表:KEERTHANAIGAL.DB" (当我提到" KEERTHANAIGAL"没有DB

时,我得到了同样的信息

如何查看数据库文件?我只复制了Firefox sqlite manager

创建的sqlite文件

0 个答案:

没有答案