I'm trying to convert several of my apps to use GRDB.swift. Does anybody have or know where I can find a file to get me started? I've read over most of the GRDB docs but I'm not getting it. Below is a sample scenario.
This I was able to convert from SQLite.sift
class Database
{
static let shared = Database()
public let databaseConnection: DatabaseQueue?
private init()
{
do
{
let fileUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("MyDaatbase.sqlite")
// GRDB
let databaseConnection = try DatabaseQueue(path: fileUrl.path)
self.databaseConnection = databaseConnection
} catch {
databaseConnection = nil
let nserror = error as NSError
print("Cannot connect to Database. Error is: \(nserror), \(nserror.userInfo)")
}
}
}
Could someone please convert this to GRDB to help me get started?
static func isAnimation() -> Bool
{
let theTable = Table("Settings")
let theColumn = Expression<String>("AnimateNav")
var theStatus = false
do {
for theAnswer in try Database.shared.databaseConnection!.prepare(theTable.select(theColumn)) {
//print(theAnswer[theColumn])
let theStatusText = (theAnswer[theColumn])
theStatus = theStatusText == "true" ? true : false
}
} catch {
print("Getting the isAnimation Status failed! Error: \(error)")
}
return theStatus
}
You can use raw SQL:
static func isAnimation() -> Bool {
var theStatus = false
do {
let animateNav = try Database.shared.databaseConnection!.read { db in
String.fetchOne(db, sql: "SELECT AnimateNav FROM Settings")
}
theStatus = animateNav == "true" ? true : false
} catch {
print("Getting the isAnimation Status failed! Error: \(error)")
}
return theStatus
}
You can also define a Record type for the Settings
table, which is the preferred GRDB way:
// Settings.swift
struct Settings: Codable, FetchableRecord, TableRecord {
var animateNav: String
// other properties for other columns in the Settings table
...
}
// Your file
static func isAnimation() -> Bool {
var theStatus = false
do {
let settings = try Database.shared.databaseConnection!.read { db in
try Settings.fetchOne(db)
}
theStatus = settings?.animateNav == "true" ? true : false
} catch {
print("Getting the isAnimation Status failed! Error: \(error)")
}
return theStatus
}