|
| 1 | +# swift-sqlite |
| 2 | + |
| 3 | +swift-sqlite is a cross-platform modernization of the [SQLite.swift] project. |
| 4 | + |
| 5 | +It includes [SQLite3] with [Full-text search] and [SQLCipher] extensions, |
| 6 | +and works out-of-the-box on macOS, iOS, Linux, Android, and Windows. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | + - A pure-Swift interface |
| 11 | + - A type-safe, optional-aware SQL expression builder |
| 12 | + - A flexible, chainable, lazy-executing query layer |
| 13 | + - Automatically-typed data access |
| 14 | + - A lightweight, uncomplicated query and parameter binding interface |
| 15 | + - Developer-friendly error handling and debugging |
| 16 | + - [Full-text search][] support |
| 17 | + - [Well-documented][See Documentation] |
| 18 | + - Extensively tested |
| 19 | + - [SQLCipher][] support |
| 20 | + - [Schema query/migration][] |
| 21 | + - Works on Android, Windows, and [Linux](Documentation/Linux.md) (with some limitations) |
| 22 | + |
| 23 | +[SQLCipher]: https://www.zetetic.net/sqlcipher/ |
| 24 | +[Full-text search]: Documentation/Index.md#full-text-search |
| 25 | +[Schema query/migration]: Documentation/Index.md#querying-the-schema |
| 26 | +[See Documentation]: Documentation/Index.md#sqliteswift-documentation |
| 27 | + |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +```swift |
| 32 | +import SQLiteDB |
| 33 | + |
| 34 | +// Wrap everything in a do...catch to handle errors |
| 35 | +do { |
| 36 | + let db = try Connection("path/to/db.sqlite3") |
| 37 | + |
| 38 | + let users = Table("users") |
| 39 | + let id = Expression<Int64>("id") |
| 40 | + let name = Expression<String?>("name") |
| 41 | + let email = Expression<String>("email") |
| 42 | + |
| 43 | + try db.run(users.create { t in |
| 44 | + t.column(id, primaryKey: true) |
| 45 | + t.column(name) |
| 46 | + t.column(email, unique: true) |
| 47 | + }) |
| 48 | + // CREATE TABLE "users" ( |
| 49 | + // "id" INTEGER PRIMARY KEY NOT NULL, |
| 50 | + // "name" TEXT, |
| 51 | + // "email" TEXT NOT NULL UNIQUE |
| 52 | + // ) |
| 53 | + |
| 54 | + let insert = users.insert(name <- "Alice", email <- "alice@mac.com") |
| 55 | + let rowid = try db.run(insert) |
| 56 | + // INSERT INTO "users" ("name", "email") VALUES ('Alice', 'alice@mac.com') |
| 57 | + |
| 58 | + for user in try db.prepare(users) { |
| 59 | + print("id: \(user[id]), name: \(user[name]), email: \(user[email])") |
| 60 | + // id: 1, name: Optional("Alice"), email: alice@mac.com |
| 61 | + } |
| 62 | + // SELECT * FROM "users" |
| 63 | + |
| 64 | + let alice = users.filter(id == rowid) |
| 65 | + |
| 66 | + try db.run(alice.update(email <- email.replace("mac.com", with: "me.com"))) |
| 67 | + // UPDATE "users" SET "email" = replace("email", 'mac.com', 'me.com') |
| 68 | + // WHERE ("id" = 1) |
| 69 | + |
| 70 | + try db.run(alice.delete()) |
| 71 | + // DELETE FROM "users" WHERE ("id" = 1) |
| 72 | + |
| 73 | + try db.scalar(users.count) // 0 |
| 74 | + // SELECT count(*) FROM "users" |
| 75 | +} catch { |
| 76 | + print (error) |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +SQLite.swift also works as a lightweight, Swift-friendly wrapper over the C |
| 81 | +API. |
| 82 | + |
| 83 | +```swift |
| 84 | +// Wrap everything in a do...catch to handle errors |
| 85 | +do { |
| 86 | + // ... |
| 87 | + |
| 88 | + let stmt = try db.prepare("INSERT INTO users (email) VALUES (?)") |
| 89 | + for email in ["betty@icloud.com", "cathy@icloud.com"] { |
| 90 | + try stmt.run(email) |
| 91 | + } |
| 92 | + |
| 93 | + db.totalChanges // 3 |
| 94 | + db.changes // 1 |
| 95 | + db.lastInsertRowid // 3 |
| 96 | + |
| 97 | + for row in try db.prepare("SELECT id, email FROM users") { |
| 98 | + print("id: \(row[0]), email: \(row[1])") |
| 99 | + // id: Optional(2), email: Optional("betty@icloud.com") |
| 100 | + // id: Optional(3), email: Optional("cathy@icloud.com") |
| 101 | + } |
| 102 | + |
| 103 | + try db.scalar("SELECT count(*) FROM users") // 2 |
| 104 | +} catch { |
| 105 | + print (error) |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +[Read the documentation][See Documentation] |
| 110 | + |
| 111 | +## Installation |
| 112 | + |
| 113 | +### Swift Package Manager |
| 114 | + |
| 115 | +The [Swift Package Manager][] is a tool for managing the distribution of |
| 116 | +Swift code. |
| 117 | + |
| 118 | +1. Add the following to your `Package.swift` file: |
| 119 | + |
| 120 | + ```swift |
| 121 | + dependencies: [ |
| 122 | + .package(url: "https://github.com/skiptools/swift-sqlite.git", "0.0.0"..<"2.0.0") |
| 123 | + ] |
| 124 | + ``` |
| 125 | + |
| 126 | +2. Build your project: |
| 127 | + |
| 128 | + ```sh |
| 129 | + $ swift build |
| 130 | + ``` |
| 131 | + |
| 132 | +[Swift Package Manager]: https://swift.org/package-manager |
| 133 | + |
| 134 | + |
| 135 | +## Communication |
| 136 | + |
| 137 | +[See the planning document] for a roadmap and existing feature requests. |
| 138 | + |
| 139 | +[Read the contributing guidelines][]. The _TL;DR_ (but please; _R_): |
| 140 | + |
| 141 | + - Need **help** or have a **general question**? [Ask on Stack |
| 142 | + Overflow][] (tag `sqlite.swift`). |
| 143 | + - Found a **bug** or have a **feature request**? [Open an issue][]. |
| 144 | + - Want to **contribute**? [Submit a pull request][]. |
| 145 | + |
| 146 | +[See the planning document]: /Documentation/Planning.md |
| 147 | +[Read the contributing guidelines]: ./CONTRIBUTING.md#contributing |
| 148 | +[Ask on Stack Overflow]: https://stackoverflow.com/questions/tagged/sqlite.swift |
| 149 | +[Open an issue]: https://github.com/skiptools/swift-sqlite/issues/new |
| 150 | +[Submit a pull request]: https://github.com/skiptools/swift-sqlite/fork |
| 151 | + |
| 152 | + |
| 153 | +## Original author |
| 154 | + |
| 155 | + - [Stephen Celis](mailto:stephen@stephencelis.com) |
| 156 | + ([@stephencelis](https://twitter.com/stephencelis)) |
| 157 | + |
| 158 | + |
| 159 | +## License |
| 160 | + |
| 161 | +SQLite.swift is available under the MIT license. See [the LICENSE |
| 162 | +file](./LICENSE.txt) for more information. |
| 163 | + |
| 164 | +## Alternatives |
| 165 | + |
| 166 | +Looking for something else? Try another Swift wrapper (or [FMDB][]): |
| 167 | + |
| 168 | + - [SQLite.swift](https://github.com/stephencelis/SQLite.swift) |
| 169 | + - [GRDB](https://github.com/groue/GRDB.swift) |
| 170 | + - [SQLiteDB](https://github.com/FahimF/SQLiteDB) |
| 171 | + - [Squeal](https://github.com/nerdyc/Squeal) |
| 172 | + |
| 173 | +[Swift]: https://swift.org/ |
| 174 | +[SQLite3]: https://www.sqlite.org |
| 175 | +[SQLite.swift]: https://github.com/stephencelis/SQLite.swift |
| 176 | +[FMDB]: https://github.com/ccgus/fmdb |
0 commit comments