lundi 27 juillet 2015

iOS SqLite. Where is the database stored

I am quite new with iOS. I try to learn how create a database with SqLite. I've been searching and I could find this tutorial: http://ift.tt/1JK1SAB

I could make it work but I have some questions. If I understand, the SqLite database is created in the ViewController.swift and they give the name contacts.db but where is that file? I do not see it in the Project Navigator, I do not see it in the files and folders either. That is the question: where is the SqLite database stored?

This is the part of the code that creates the database:

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

Aucun commentaire:

Enregistrer un commentaire