Github url

SQLite.swift

by stephencelis

stephencelis /SQLite.swift

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

7.0K Stars 1.2K Forks Last release: about 1 year ago (0.12.2) MIT License 860 Commits 15 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

SQLite.swift

Build Status CocoaPods Version Swift5 compatible Platform Carthage compatible Join the chat at https://gitter.im/stephencelis/SQLite.swift

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
  • Developer-friendly error handling and debugging
  • Full-text search support
  • Well-documented
  • Extensively tested
  • SQLCipher support via CocoaPods
  • Active support atStackOverflow, and Gitter Chat Room(experimental)

Usage

import SQLite let db = try Connection("path/to/db.sqlite3") let users = Table("users") let id = Expression<int64>("id")
let name = Expression<string>("name")
let email = Expression<string>("email")

try db.run(users.create { 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
// )

let insert = users.insert(name 
<p>SQLite.swift also works as a lightweight, Swift-friendly wrapper over the C
API.</p>
<pre class="swift">let stmt = try db.prepare("INSERT INTO users (email) VALUES (?)")
for email in ["[email protected]", "[email protected]"] {
    try stmt.run(email)
}

db.totalChanges // 3
db.changes // 1
db.lastInsertRowid // 3

for row in try db.prepare("SELECT id, email FROM users") {
    print("id: \(row[0]), email: \(row[1])")
    // id: Optional(2), email: Optional("[email protected]")
    // id: Optional(3), email: Optional("[email protected]")
}

try db.scalar("SELECT count(*) FROM users") // 2
</pre>
<p><a href="https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#sqliteswift-documentation">Read the documentation</a> or explore more,
interactively, from the Xcode project’s playground.</p>

<p><img src="https://github.com/stephencelis/SQLite.swift/raw/master/Documentation/Resources/[email protected]" alt="SQLite.playground Screen Shot"></p>

<p>For a more comprehensive example, see
<a href="http://masteringswift.blogspot.com/2015/09/create-data-access-layer-with.html">this article</a>
and the <a href="https://github.com/hoffmanjon/SQLiteDataAccessLayer2/tree/master">companion repository</a>.</p>

<h2>Installation</h2>

<blockquote>
<p><em>Note:</em> Version 0.12 requires Swift 5 (and <a href="https://developer.apple.com/xcode/downloads/">Xcode</a> 10.2) or greater. Version 0.11.6 requires Swift 4.2 (and <a href="https://developer.apple.com/xcode/downloads/">Xcode</a> 10.1) or greater.</p>
</blockquote>

<h3>Carthage</h3>

<p><a href="https://github.com/Carthage/Carthage">Carthage</a> is a simple, decentralized dependency manager for Cocoa. To
install SQLite.swift with Carthage:</p>

<ol>
<li><p>Make sure Carthage is <a href="https://github.com/Carthage/Carthage#installing-carthage">installed</a>.</p></li>
<li>
<p>Update your Cartfile to include the following:</p>
<pre class="ruby">github "stephencelis/SQLite.swift" ~&gt; 0.12.0
</pre>
</li>
<li>
<p>Run </p>
<pre class="">carthage update</pre> and
<a href="https://github.com/Carthage/Carthage#adding-frameworks-to-an-application">add the appropriate framework</a>.</li>
</ol>

<h3>CocoaPods</h3>

<p><a href="https://cocoapods.org">CocoaPods</a> is a dependency manager for Cocoa projects. To install
SQLite.swift with CocoaPods:</p>

<ol>
<li>
<p>Make sure CocoaPods is <a href="https://guides.cocoapods.org/using/getting-started.html#getting-started">installed</a>. (SQLite.swift
requires version 1.6.1 or greater.)</p>
<pre class="sh"># Using the default Ruby install will require you to use sudo when
# installing and updating gems.
[sudo] gem install cocoapods
</pre>
</li>
<li>
<p>Update your Podfile to include the following:</p>
<pre class="ruby">use_frameworks!

target 'YourAppTargetName' do
    pod 'SQLite.swift', '~&gt; 0.12.0'
end
</pre>
</li>
<li>
<p>Run </p>
<pre class="">pod install --repo-update</pre>.</li>
</ol>

<h3>Swift Package Manager</h3>

<p>The <a href="https://swift.org/package-manager">Swift Package Manager</a> is a tool for managing the distribution of
Swift code.</p>

<ol>
<li>Add the following to your <pre class="">Package.swift</pre> file:</li>
</ol>
<pre class="swift"> dependencies: [
      .package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.12.0")
  ]
</pre>
<ol>
<li>Build your project:</li>
</ol>
<pre class="sh"> $ swift build
</pre>
<h3>Manual</h3>

<p>To install SQLite.swift as an Xcode sub-project:</p>

<ol>
<li>
<p>Drag the <strong>SQLite.xcodeproj</strong> file into your own project.
(<a href="http://git-scm.com/book/en/Git-Tools-Submodules">Submodule</a>, clone, or <a href="https://github.com/stephencelis/SQLite.swift/archive/master.zip">download</a> the project first.)</p>

<p><img src="https://github.com/stephencelis/SQLite.swift/raw/master/Documentation/Resources/[email protected]" alt="Installation Screen Shot"></p>
</li>
<li><p>In your target’s <strong>General</strong> tab, click the <strong>+</strong> button under <strong>Linked
Frameworks and Libraries</strong>.</p></li>
<li><p>Select the appropriate <strong>SQLite.framework</strong> for your platform.</p></li>
<li><p><strong>Add</strong>.</p></li>
</ol>

<p>Some additional steps are required to install the application on an actual
device:</p>

<ol>
<li><p>In the <strong>General</strong> tab, click the <strong>+</strong> button under <strong>Embedded
Binaries</strong>.</p></li>
<li><p>Select the appropriate <strong>SQLite.framework</strong> for your platform.</p></li>
<li><p><strong>Add</strong>.</p></li>
</ol>

<h2>Communication</h2>

<p><a href="https://github.com/stephencelis/SQLite.swift/blob/master//Documentation/Planning.md">See the planning document</a> for a roadmap and existing feature requests.</p>

<p><a href="https://github.com/stephencelis/SQLite.swift/blob/master/./CONTRIBUTING.md#contributing">Read the contributing guidelines</a>. The <em>TL;DR</em> (but please; <em>R</em>):</p>

<ul>
<li>Need <strong>help</strong> or have a <strong>general question</strong>? <a href="http://stackoverflow.com/questions/tagged/sqlite.swift">Ask on Stack
Overflow</a> (tag <pre class="">sqlite.swift</pre>).</li>
<li>Found a <strong>bug</strong> or have a <strong>feature request</strong>? <a href="https://github.com/stephencelis/SQLite.swift/issues/new">Open an issue</a>.</li>
<li>Want to <strong>contribute</strong>? <a href="https://github.com/stephencelis/SQLite.swift/fork">Submit a pull request</a>.</li>
</ul>

<h2>Author</h2>

<ul>
<li>
<a href="mailto:[email protected]">Stephen Celis</a>
(<a href="https://twitter.com/stephencelis">@stephencelis</a>)</li>
</ul>

<h2>License</h2>

<p>SQLite.swift is available under the MIT license. See <a href="https://github.com/stephencelis/SQLite.swift/blob/master/./LICENSE.txt">the LICENSE
file</a> for more information.</p>

<h2>Related</h2>

<p>These projects enhance or use SQLite.swift:</p>

<ul>
<li>
<a href="https://github.com/garriguv/SQLiteMigrationManager.swift">SQLiteMigrationManager.swift</a> (inspired by
<a href="https://github.com/layerhq/FMDBMigrationManager">FMDBMigrationManager</a>)</li>
</ul>

<h2>Alternatives</h2>

<p>Looking for something else? Try another Swift wrapper (or <a href="https://github.com/ccgus/fmdb">FMDB</a>):</p>

<ul>
<li><a href="https://github.com/remirobert/Camembert">Camembert</a></li>
<li><a href="https://github.com/groue/GRDB.swift">GRDB</a></li>
<li><a href="https://github.com/FahimF/SQLiteDB">SQLiteDB</a></li>
<li><a href="https://github.com/nerdyc/Squeal">Squeal</a></li>
<li><a href="https://github.com/ryanfowler/SwiftData">SwiftData</a></li>
<li><a href="https://github.com/chrismsimpson/SwiftSQLite">SwiftSQLite</a></li>
</ul>
</string></string></int64>

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.