Swift – 设备上未包含预先填充的SQLite数据库

我试图将一个预先填充的sqlite数据库(带有FMDB包装器)包含在我的物理iPhone的构建中.但是,预先填充的数据库未包含在物理设备的构建中.

请注意,它在IOS模拟器中工作正常,但仅适用于一个模拟器设备.

我已将数据库包含在Build Phases>中复制捆绑资源并将Link Binary下的libsqlite3.dylib与Libraries联系起来.

我需要添加什么Swift代码才能将构建中包含的数据库添加到物理设备中?

码:

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var checkButton: UIButton!
    @IBOutlet weak var tests_scroller: UITextView!

    var databasePath = Nsstring()

    override func viewDidLoad() {
        super.viewDidLoad()

        checkButton.setTitle("\u{2610}",forState: .normal)
        checkButton.setTitle("\u{2611}",forState: .Selected)


        let filemgr = NSFileManager.defaultManager()

        let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)

        let docsDir = dirPaths[0] as! String

        databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")

        let myDatabase = FMDatabase(path: databasePath as String)


        if myDatabase.open(){

            var arrayData:[String] = []

            let query_lab_test = "SELECT lab_test FROM lab_test ORDER BY lab_test ASC"

            let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test,withArgumentsInArray: nil)

            while results_lab_test?.next() == true {

                if let resultString = results_lab_test?.stringForColumn("lab_test"){

                arrayData.append(resultString)

            }
        }
            var multiLinestring = join("\n",arrayData)
            tests_scroller.text = multiLinestring
            myDatabase.close()
    }
    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }
}

更新 – 在XCode 7中使用Swift 2代码 – 使用FMDB包装器预先填充的sqlite数据库复制到物理设备OK:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tests_scroller: UITextView!

    var databasePath = Nsstring()

    override func viewDidLoad() {
        super.viewDidLoad()


        let sourcePath = NSBundle.mainBundle().pathForResource("vmd_db",ofType: "db")

        let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,true).first as String!

        let destinationPath = (documentDirectoryPath as Nsstring).stringByAppendingPathComponent("vmd_db.db")

        do {                                
            try NSFileManager().copyItemAtPath(sourcePath!,toPath: destinationPath)

        } catch _ {                
        }

        //read it
        let myDatabase = FMDatabase(path: destinationPath as String)

        if myDatabase.open(){

            var arrayData:[String] = []

            let query_lab_test = "SELECT lab_test FROM lab_test ORDER BY lab_test ASC"

            let results_lab_test:FMResultSet? = myDatabase.executeQuery(query_lab_test,withArgumentsInArray: nil)

            while results_lab_test?.next() == true {

                if let resultString = results_lab_test?.stringForColumn("lab_test"){

                    arrayData.append(resultString)

                }
            }

            let multiLinestring = arrayData.joinWithSeparator("\n")

            tests_scroller.text = multiLinestring
            myDatabase.close()
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // dispose of any resources that can be recreated.
    }
}

解决方法

从我看到它不应该在任何模拟器/设备上工作,因为您访问文档文件夹中的文件.那不在你的包中.

您需要做的是将文件从包中复制到文档目录,然后尝试在那里打开它:

//path in documents
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,true)
let docsDir = dirPaths[0]
databasePath = docsDir.stringByAppendingPathComponent("vmd_db.db")

//if not there,copy from bundle
if !filemgr.fileExistsAtPath(databasePath) {
    let bundleDatabasePath = NSBundle.mainBundle().pathForResource("vm_db",ofType: "db")
    filemgr.copyItemAtPath(bundleDatabasePath,toPath: databasePath)
}

//read it
let myDatabase = FMDatabase(path: databasePath as String)

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...