Search code examples
iosswiftgrdb

DatabaseConnecton issue in GRDB swift


I would like to use SQLite database in my iOS app and there are different frameworks available. So my current focus is on GRDB and I have successfully created, inserted values, and received back with SQL commands as shown here.

However, once the app is closed, how am I going to access the same DataBaseQueue and what should be the constant path to my database?

import GRDB

// Open a simple database connection

if let dbQueue = try DatabaseQueue(path: "what should be the correct path ")!= nil{
    setupDatabase()
}
else
{
    dbQueue = DatabaseQueue()
}

Solution

  • The GRDB FAQ answers your question:

    How do I create a database in my application?

    The database has to be stored in a valid place where it can be created and modified. For example, in the Application Support directory:

    let databaseURL = try FileManager.default
        .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        .appendingPathComponent("db.sqlite")
    let dbQueue = try DatabaseQueue(path: databaseURL.path)