将应用程序从 SQLite.swift 转换为 GRDB.swift
Converting app from SQLite.swift to GRDB.swift
我正在尝试将我的几个应用程序转换为使用 GRDB.swift。有没有人知道或知道我在哪里可以找到一个文件来帮助我开始?我已经阅读了大部分 GRDB 文档,但我不明白。下面是一个示例场景。
我可以从 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)")
}
}
}
有人可以将其转换为 GRDB 以帮助我开始吗?
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
}
您可以使用原始 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
}
你也可以为Settings
table定义一个Record type,这是首选的GRDB方式:
// 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
}
我正在尝试将我的几个应用程序转换为使用 GRDB.swift。有没有人知道或知道我在哪里可以找到一个文件来帮助我开始?我已经阅读了大部分 GRDB 文档,但我不明白。下面是一个示例场景。
我可以从 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)")
}
}
}
有人可以将其转换为 GRDB 以帮助我开始吗?
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
}
您可以使用原始 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
}
你也可以为Settings
table定义一个Record type,这是首选的GRDB方式:
// 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
}