【问题标题】:iOS SqLite and Swift. Where is the database storediOS SqlLite 和 Swift。数据库存储在哪里
【发布时间】:2015-10-17 10:21:42
【问题描述】:

我对 iOS 很陌生。我尝试学习如何使用 SqlLite 创建数据库。我一直在寻找,我可以找到这个教程: www.techotopia.com/index.php/An_Example_SQLite_based_iOS_8_Application_using_Swift_and_FMDB

我可以让它工作,但我有一些问题。如果我理解的话,SqlLite 数据库是在 ViewController.swift 中创建的,它们的名称是 contacts.db,但该文件在哪里?我在项目导航器中看不到它,我也没有在文件和文件夹中看到它。这就是问题所在:SqlLite 数据库存储在哪里?

这是创建数据库的部分代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let filemgr = NSFileManager.defaultManager()
    let dirPaths =
    NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
            .UserDomainMask, true)

    let docsDir = dirPaths[0] as! String

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

    if !filemgr.fileExistsAtPath(databasePath as String) {

        let contactDB = FMDatabase(path: databasePath as String)

        if contactDB == nil {
            println("Error: \(contactDB.lastErrorMessage())")
        }

        if contactDB.open() {
            let sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"
            if !contactDB.executeStatements(sql_stmt) {
                println("Error: \(contactDB.lastErrorMessage())")
            }
            contactDB.close()
        } else {
            println("Error: \(contactDB.lastErrorMessage())")
        }
    }
}

【问题讨论】:

标签: ios swift sqlite


【解决方案1】:

只需在控制台中打印您的路径:

let docsDir = dirPaths[0] as! String

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

println(databasePath)

它将在您的CoreSimulator 目录中。

在您的控制台上打印您的路径后。您可以使用 Finder 中的 Go > Go To Folder... 命令。

在 iOS 7 中,我们在

处提供了应用程序文件夹

库>应用程序支持>iPhone 模拟器

窗体 iOS 8

库>开发者/CoreSimulator

【讨论】:

  • 我把你的代码放在 ViewController.swift 中,它给了我一个错误。我很新,可能我做错了
  • 我将您的代码复制粘贴到 ViewController.swift 中,它在第 2 行显示预期声明
  • 是的,完美。抱歉,我是新手
  • 为什么我在mac中简单搜索contacts.db找不到?
  • @Nrc 你可以查看这个问题。 stackoverflow.com/questions/24133022/… 可以得到帮助。
猜你喜欢
  • 1970-01-01
  • 2019-12-22
  • 2010-11-11
  • 1970-01-01
  • 2010-11-09
  • 2011-01-06
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多