ios - 为什么要创建一个空数据库(0 口)?

标签 ios database swift sqlite xcode6

我的应用程序使用了 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/

相关文章:

iOS如何在首页隐藏nagationBar,其他viewcontroller显示navgationBar

ios - 设置后台 session 的 xpc 连接发生错误

ios - 无法更新 MFSideMenu 中的个人资料图片

iphone - MapKit MKMapView "funny"蓝色光晕崩溃

iphone - 模拟器在 Xcode 4.4 中不断崩溃

mysql - 需要在 MYSQL V5.1.11 中将 sql 转储存储在其他架构中

mysql - 无法在 MySQL 5.5.25 中设置 max_allowed_pa​​cket

database - 用户信息和登录凭据的表设计?

ios - Swift3 UITextField 更改占位符字体类型和大小

ios - 在 UITableViews 中设置 Navbar Translucent To Start 动画