2

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: www.techotopia.com/index.php/An_Example_SQLite_based_iOS_8_Application_using_Swift_and_FMDB

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())")
        }
    }
}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Nrc
  • 9,577
  • 17
  • 67
  • 114
  • possible duplicate of [IOS 8 Store sqlite File Location Core Data](http://stackoverflow.com/questions/24133022/ios-8-store-sqlite-file-location-core-data) – Ruben Steins Jul 27 '15 at 09:35
  • check out this link http://stackoverflow.com/questions/27834238/where-does-sqlite-file-get-stored-on-mac – Nishant Gupta Jul 27 '15 at 09:39

2 Answers2

3

Just print your path in your console :

let docsDir = dirPaths[0] as! String

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

println(databasePath)

It will be in your CoreSimulator directory.

After printing your path on your console. You can use the Go > Go To Folder... Command from the Finder.

In iOS 7 We have applications folders available at

Library>Application Support>iPhone Simulator

Form iOS 8

Library>Developer/CoreSimulator

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
  • I put your code in the ViewController.swift and it gives me an error. I am very new, probably I am doing it wrong – Nrc Jul 27 '15 at 09:45
  • I copy-paste your code in ViewController.swift and it says Expected declaration in line 2 – Nrc Jul 27 '15 at 09:54
  • Yes, perfect. And sorry, I am very new – Nrc Jul 27 '15 at 10:06
  • Why I could not find it if I make a simple search in the mac for contacts.db? – Nrc Jul 27 '15 at 10:08
  • @Nrc You can check this question. http://stackoverflow.com/questions/24133022/ios-8-store-sqlite-file-location-core-data may you get help. – Ashish Kakkad Jul 27 '15 at 10:38
0
func getSQLitePath() -> String? {
    // Get the URL for the app's "Documents" directory
    if let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
        let databaseURL = documentsDirectory.appendingPathComponent("myDb.sqlite")
        return databaseURL.path
    }
    return nil
} 

// Example usage:

    if let path = getSQLitePath() {
        print("SQLite database path: \(path)")
    } else {
        print("SQLite database not found.")
    }
M Hamayun zeb
  • 448
  • 4
  • 10