簡體   English   中英

iOS SqLite和Swift。 數據庫存儲在哪里

[英]iOS SqLite and Swift. Where is the database stored

我對iOS很陌生。 我嘗試學習如何使用SqLite創建數據庫。 我一直在搜索,並且可以找到本教程:www.techotopia.com/index.php/An_Example_SQLite_based_iOS_8_Application_using_Swift_and_FMDB

我可以使它工作,但是我有一些疑問。 據我了解,SqLite數據庫是在ViewController.swift中創建的,它們的名稱為contacts.db,但是該文件在哪里? 我在“項目瀏覽器”中沒有看到它,在文件和文件夾中也沒有看到它。 問題是:SqLite數據庫存儲在哪里?

這是創建數據庫的代碼的一部分:

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())")
        }
    }
}

只需在控制台中打印路徑:

let docsDir = dirPaths[0] as! String

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

println(databasePath)

它將在您的CoreSimulator目錄中。

在控制台上打印路徑后。 您可以從Finder使用“ 轉到”>“轉到文件夾...”命令。

在iOS 7中,我們在以下位置提供了應用程序文件夾

庫>應用程序支持> iPhone Simulator

表格iOS 8

圖書館>開發商/ CoreSimulator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM