INACTIVE - A type-safe, Swift-language layer over SQLite3.
Перейти к файлу
Stephen Celis a7cf5f2f0d Update documentation to reflect Bool's current status
Signed-off-by: Stephen Celis <stephen@stephencelis.com>
2015-02-04 20:59:13 -08:00
Documentation Update documentation to reflect Bool's current status 2015-02-04 20:59:13 -08:00
SQLite Common Add support for custom SQL functions 2015-02-04 20:03:36 -08:00
SQLite Common Tests Add support for custom SQL functions 2015-02-04 20:03:36 -08:00
SQLite Mac SQLite.swift 2014-10-05 18:29:39 -07:00
SQLite Mac Tests SQLite.swift 2014-10-05 18:29:39 -07:00
SQLite iOS SQLite.swift 2014-10-05 18:29:39 -07:00
SQLite iOS Tests SQLite.swift 2014-10-05 18:29:39 -07:00
SQLite.playground Remove Bool support from thin SQLite layer 2015-02-03 20:36:07 -08:00
SQLite.xcodeproj Add support for custom SQL functions 2015-02-04 20:03:36 -08:00
.gitignore SQLite.swift 2014-10-05 18:29:39 -07:00
.travis.yml Update Travis CI configuration 2015-01-27 09:27:01 -08:00
LICENSE.txt SQLite.swift 2014-10-05 18:29:39 -07:00
Makefile Expose REPL errors 2014-12-15 13:38:14 -08:00
README.md Show Travis CI badge for master branch 2015-01-17 12:50:43 -08:00

README.md

SQLite.swift Build Status

A type-safe, Swift-language layer over SQLite3.

SQLite.swift provides compile-time confidence in SQL statement syntax and intent.

Features

  • A pure-Swift interface
  • A type-safe, optional-aware SQL expression builder
  • A flexible, chainable, lazy-executing query layer
  • Automatically-typed data access
  • A lightweight, uncomplicated query and parameter binding interface
  • Transactions with implicit commit/rollback
  • Developer-friendly error handling and debugging
  • Well-documented
  • Extensively tested

Usage

import SQLite

let db = Database("path/to/db.sqlite3")

let users = db["users"]
let id = Expression<Int>("id")
let name = Expression<String?>("name")
let email = Expression<String>("email")

db.create(table: users) { t in
    t.column(id, primaryKey: true)
    t.column(name)
    t.column(email, unique: true)
}
// CREATE TABLE "users" (
//     "id" INTEGER PRIMARY KEY NOT NULL,
//     "name" TEXT,
//     "email" TEXT NOT NULL UNIQUE
// )

var alice: Query?
if let insertedID = users.insert(name <- "Alice", email <- "alice@mac.com") {
    println("inserted id: \(insertedID)")
    // inserted id: 1
    alice = users.filter(id == insertedID)
}
// INSERT INTO "users" ("name", "email") VALUES ('Alice', 'alice@mac.com')

for user in users {
    println("id: \(user[id]), name: \(user[name]), email: \(user[email])")
    // id: 1, name: Optional("Alice"), email: alice@mac.com
}
// SELECT * FROM "users"

alice?.update(email <- replace(email, "mac.com", "me.com"))?
// UPDATE "users" SET "email" = replace("email", 'mac.com', 'me.com')
// WHERE ("id" = 1)

alice?.delete()?
// DELETE FROM "users" WHERE ("id" = 1)

users.count
// SELECT count(*) FROM "users"

SQLite.swift also works as a lightweight, Swift-friendly wrapper over the C API.

let stmt = db.prepare("INSERT INTO users (email) VALUES (?)")
for email in ["betty@icloud.com", "cathy@icloud.com"] {
    stmt.run(email)
}

db.totalChanges // 3
db.lastChanges  // {Some 1}
db.lastID       // {Some 3}

for row in db.prepare("SELECT id, email FROM users") {
    println("id: \(row[0]), email: \(row[1])")
    // id: Optional(2), email: Optional("betty@icloud.com")
    // id: Optional(3), email: Optional("cathy@icloud.com")
}

db.scalar("SELECT count(*) FROM users") // {Some 2}

Read the documentation or explore more, interactively, from the Xcode projects playground.

SQLite.playground Screen Shot

Installation

Note: SQLite.swift requires Swift 1.1 (and Xcode 6.1) or greater.

To install SQLite.swift:

  1. Drag the SQLite.xcodeproj file into your own project. (Submodule, clone, or download the project first.)

  2. In your targets Build Phases, add SQLite iOS (or SQLite Mac) to the Target Dependencies build phase.

  3. Add the appropriate SQLite.framework product to the Link Binary With Libraries build phase.

  4. Add the same SQLite.framework to a Copy Files build phase with a Frameworks destination. (Add a new build phase if need be.)

Communication

Author

License

SQLite.swift is available under the MIT license. See the LICENSE file for more information.

Alternatives

Looking for something else? Try another Swift wrapper (or FMDB):