Skip to content

SQLiteExpress is a small Swift 5 library implementing a wrapper for the C API of SQLite3 on macOS and iOS.

License

Notifications You must be signed in to change notification settings

objecthub/swift-sqliteexpress

Repository files navigation

SQLiteExpress

Platforms: macOS, iOS Language: Swift 5.3 IDE: Xcode 12.0 Package managers: SwiftPM, Carthage License: Apache

Overview

SQLiteExpress is a Swift library implementing a lightweight wrapper for the C API of SQLite3 on macOS and iOS. SQLiteExpress provides

  • an object-oriented data model,
  • a clear separation of errors, expressed as exceptions, from results and side-effects, and
  • uses Swift programming paradigms throughout its implementation.

This is done without changing the programming protocol of the C API of SQLite3. SQLiteEpress does not provide any help in crafting SQL statements, e.g. in a type-safe manner. If this functionality is needed, other SQLite wrappers are more suitable.

Usage example

The following code snippet gives a flavor of how SQLiteExpress is used. The code snippet does the following things:

  1. It creates a new in-memory database
  2. It creates a new table in the database
  3. It enters data into the new table
  4. It queries the new table for some data
// Create a new in-memory database `db` and require extended errors to be thrown.
let db = try SQLiteDatabase(extendedErrors: true)

// Create a new table in `db`.
let stmt0 = try db.prepare(sql: """
    CREATE TABLE Contacts (
      id INTEGER PRIMARY KEY,
      name TEXT NOT NULL,
      email TEXT NOT NULL UNIQUE,
      phone TEXT
    );
  """)
try stmt0.step()
stmt0.finalize()

// Enter two rows into the new table.
let stmt1 = try db.prepare(sql: """
    INSERT INTO Contacts VALUES (?, ?, ?, ?);
  """)
try stmt1.bind(integer: 1000, toParam: 1)
try stmt1.bind(text: "Mickey Mouse", toParam: 2)
try stmt1.bind(text: "[email protected]", toParam: 3)
try stmt1.bind(text: "+1 101-123-456", toParam: 4)
try stmt1.step()
try stmt1.bind(integer: 1001, toParam: 1)
try stmt1.bind(text: "Donald Duck", toParam: 2)
try stmt1.bind(text: "[email protected]", toParam: 3)
try stmt1.bind(text: "+1 101-123-456", toParam: 4)
try stmt1.step()
stmt1.finalize()

// Cound the number of distinct phone numbers.
let stmt2 = try db.prepare(sql: """
    SELECT COUNT(DISTINCT phone) FROM Contacts;
  """)
try stmt2.step()
let phoneNumbers = try stmt2.int(atColumn: 0)
Swift.print("Number of distinct phone numbers: \(phoneNumbers)")
stmt2.finalize()

// Show all names and email addresses from the `Contacts` table.
let stmt3 = try db.prepare(sql: """
    SELECT name, email FROM Contacts;
  """)
Swift.print("Entries in table:")
while try !stmt3.step() {
 Swift.print("\(try stmt3.text(atColumn: 0)!), \(try stmt3.text(atColumn: 1)!)")
}
stmt3.finalize()

When executed, the code prints out the following lines:

Number of distinct phone numbers: 1
Entries in table:
Mickey Mouse, [email protected]
Donald Duck, [email protected]

Requirements

The following technologies are needed to build the components of the Swift SQLiteExpress framework:

Copyright

Author: Matthias Zenger ([email protected])
Copyright © 2020 Google LLC.
Please note: This is not an official Google product.

About

SQLiteExpress is a small Swift 5 library implementing a wrapper for the C API of SQLite3 on macOS and iOS.

Resources

License

Stars

Watchers

Forks

Packages

No packages published