SQLite.swift README

The project's README from github.com/stephencelis/SQLite.swift. As one markdown file: README.md.

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

Usage

import SQLite

// Wrap everything in a do...catch to handle errors
do {
    let db = try Connection("path/to/db.sqlite3")

    let users = Table("users")
    let id = SQLite.Expression<Int64>("id")
    let name = SQLite.Expression<String?>("name")
    let email = SQLite.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 <- "Alice", email <- "[email protected]")
    let rowid = try db.run(insert)
    // INSERT INTO "users" ("name", "email") VALUES ('Alice', '[email protected]')

    for user in try db.prepare(users) {
        print("id: \(user[id]), name: \(user[name]), email: \(user[email])")
        // id: 1, name: Optional("Alice"), email: [email protected]
    }
    // SELECT * FROM "users"

    let alice = users.filter(id == rowid)

    try db.run(alice.update(email <- email.replace("mac.com", with: "me.com")))
    // UPDATE "users" SET "email" = replace("email", 'mac.com', 'me.com')
    // WHERE ("id" = 1)

    try db.run(alice.delete())
    // DELETE FROM "users" WHERE ("id" = 1)

    try db.scalar(users.count) // 0
    // SELECT count(*) FROM "users"
} catch {
    print (error)
}

Note that Expression should be written as SQLite.Expression to avoid conflicts with the SwiftUI.Expression if you are using SwiftUI too.

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

// Wrap everything in a do...catch to handle errors
do {
    // ...

    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
} catch {
    print (error)
}

Read the documentation or explore more, interactively, from the Xcode project’s playground.

SQLite.playground Screen Shot

Installation

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code.

  1. Add the following to your Package.swift file:
dependencies: [
    .package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.16.0")
]
  1. Build your project:
$ swift build

See the Tests/SPM folder for a small demo project which uses SPM.

Carthage

Carthage is a simple, decentralized dependency manager for Cocoa. To install SQLite.swift with Carthage:

  1. Make sure Carthage is installed.

  2. Update your Cartfile to include the following:

    github "stephencelis/SQLite.swift" ~> 0.16.0
    
  3. Run carthage update and add the appropriate framework.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. To install SQLite.swift with CocoaPods:

  1. Make sure CocoaPods is installed.

    # Using the default Ruby install will require you to use sudo when
    # installing and updating gems.
    [sudo] gem install cocoapods
    
  2. Update your Podfile to include the following:

    use_frameworks!
    
    target 'YourAppTargetName' do
        pod 'SQLite.swift', '~> 0.15.0'
    end
    
  3. Run pod install --repo-update.

Manual

To install SQLite.swift as an Xcode sub-project:

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

    Installation Screen Shot

  2. In your target’s General tab, click the + button under Linked Frameworks and Libraries.

  3. Select the appropriate SQLite.framework for your platform.

  4. Add.

Some additional steps are required to install the application on an actual device:

  1. In the General tab, click the + button under Embedded Binaries.

  2. Select the appropriate SQLite.framework for your platform.

  3. Add.

Communication

Read the contributing guidelines. The TL;DR (but please; R):

Original author

License

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

These projects enhance or use SQLite.swift:

Alternatives

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

SQLite.swift (stephencelis/SQLite.swift)

Searching Database…

这是什么?SQLite.swift 是一个类型安全的 Swift 语言封装层,基于 SQLite3,让你可以编写并运行 SQL 查询,并在编译时进行检查,而不必直接使用原始的 SQL 字符串。
这是一个完整的 ORM,还是只是对 C API 的薄封装?它是一个库,而不是完整的 ORM:它提供可链式调用、惰性执行的查询构建器和带类型的数据访问;需要时,也可以直接把它当作对 SQLite C API 的轻量、符合 Swift 习惯的封装来使用。
我该如何把它添加到项目中?可以使用 Swift Package Manager:在 Package.swift 中添加它后构建;也可以通过 Carthage/CocoaPods 安装;或者手动将 SQLite.xcodeproj 拖入你的 Xcode 项目,并链接框架。
我需要 Xcode 或特定的 Swift 版本吗?你需要 Xcode(或 Swift Package Manager/Carthage/CocoaPods 等工具),以及 Swift 5,因为该项目标注为兼容 Swift 5。
Linux 支持是否完整?它可以在 Linux 上运行,但 README 明确指出,与 Apple 平台相比存在一些限制。
它只能在 Apple 平台上运行吗?它主要为 Apple 平台(iOS/macOS,可通过 CocoaPods/Carthage/Xcode 安装)而构建,但也能在 Linux 上运行,只是有一些需要注意的地方。
支持全文搜索吗?支持。它内置了全文搜索功能,并且是其官方文档中列出的特性之一。
可以处理加密数据库吗?可以。它支持通过 Swift Package Manager 使用 SQLCipher,以处理加密的 SQLite 数据库。
它能处理数据库结构迁移吗?可以。它提供结构查询和迁移支持,还有一个配套项目 SQLiteMigrationManager.swift,专门用于数据库迁移。
如果这个不适合我的需求,有哪些替代方案?README 推荐了其他 Swift SQLite 封装库作为替代方案,包括 GRDB、SQLiteDB 和 FMDB。
Size
4.1 MB
Stars
10.2K
Age
12y
Commits · 90 Days