Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

ios - Converting app from SQLite.swift to GRDB.swift

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
    }
question from:https://stackoverflow.com/questions/65545884/converting-app-from-sqlite-swift-to-grdb-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...