Swiftpack.co - apparata/SQLiteKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by apparata.
apparata/SQLiteKit 1.1.0
Simple Swift wrapper for accessing a SQLite database in a thread safe manner.
⭐️ 0
🕓 29 weeks ago
iOS macOS tvOS
.package(url: "https://github.com/apparata/SQLiteKit.git", from: "1.1.0")

SQLiteKit

SwiftPM compatible MIT License language Swift 5.1 platform macOS platform Linux

Simple Swift wrapper for accessing a .sqlite3 database in a thread safe manner.

License

SQLiteKit is public domain. See UNLICENSE file for details.

Table of Contents

Getting Started

Add SQLiteKit to your Swift package by adding the following to your Package.swift file in the dependencies array:

.package(url: "https://github.com/apparata/SQLiteKit.git", from: "<version>")

Example

import Foundation
import SQLiteKit

let databasePath = "/tmp/testdb.sqlite3"

do {
    let dbQueue = try SQLQueue.open(path: databasePath)
    
    dbQueue.didUpdate = { type, table, rowID in
        print("Did \(type) table \(table ?? "") rowID \(rowID)")
    }
    
    try dbQueue.runSynchronously { db in
        try db.execute(sql: "CREATE TABLE IF NOT EXISTS Car (carID INTEGER PRIMARY KEY ASC, make TEXT NOT NULL, color TEXT NOT NULL);")
    }

    var insertCar: SQLStatement?
    
    try dbQueue.runSynchronously { db in
        insertCar = try db.prepare(statement: "INSERT INTO Car (make, color) VALUES (?, ?);")
    }
    
    dbQueue.transaction { db in
        
        try insertCar?.resetBindAndStep(values: .text("Ford"), .text("Red"))
        try insertCar?.resetBindAndStep(values: .text("Ferrari"), .text("Green"))
        try insertCar?.resetBindAndStep(values: .text("Volvo"), .text("Blue"))

        return .commit
    }
    
    RunLoop.main.run(until: Date().addingTimeInterval(2))
    
} catch {
    dump(error)
    exit(1)
}

Declarative Table Query Example

let table = SQLTable("Cars") {
    SQLColumn("brand", String.self).notNull()
    SQLColumn("model", Int.self).notNull()
    SQLColumn("doorCount", Int.self).notNull()
    SQLColumn("weight", Double.self).notNull()
}
.primaryKey("brand", "model", onConflict: .replace)

let query: SQLQuery = SQLQuery.createTable(table)

print(query.string)

GitHub

link
Stars: 0
Last commit: 29 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.1.0: Added backup functions
29 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics