Skip to content

Repository files navigation

SQLiteExpress

Platforms: macOS, iOSLanguage: Swift 5.3IDE: Xcode 12.0Package managers: SwiftPM, CarthageLicense: 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.
letdb=trySQLiteDatabase(extendedErrors:true)
// Create a new table in `db`.
letstmt0=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.
letstmt1=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:"mickey@disney.net", 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:"donald@disney.net", toParam:3)try stmt1.bind(text:"+1 101-123-456", toParam:4)try stmt1.step()
stmt1.finalize()
// Cound the number of distinct phone numbers.
letstmt2=try db.prepare(sql:""" SELECT COUNT(DISTINCT phone) FROM Contacts;""")try stmt2.step()letphoneNumbers=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.
letstmt3=try db.prepare(sql:""" SELECT name, email FROM Contacts;""")Swift.print("Entries in table:")whiletry!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, mickey@disney.net
Donald Duck, donald@disney.net

Requirements

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

Copyright

Author: Matthias Zenger (matthias@objecthub.net)
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

Contributing

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages