0

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()
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Wajdan Ali
  • 179
  • 13

1 Answers1

0

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)
Gwendal Roué
  • 3,949
  • 15
  • 34
  • ok so I didnot know that let dbQueue = try DatabaseQueue(path: databaseURL.path) wasnt only used for retrieval of database, but it also creates one when there isnt available. Thankyou for the clarification sir – Wajdan Ali Nov 14 '17 at 17:44
  • Yes, this is true. `DatabaseQueue(path:...)` creates the database if it does not exist yet, and opens it. – Gwendal Roué Nov 14 '17 at 18:10
  • It's actually an SQLite feature, not a GRDB feature! – Gwendal Roué Nov 14 '17 at 18:11