欢迎加入QQ讨论群258996829

SQLite.swift

发布时间:2016-01-05 15:14  回复:0  查看:4251  感兴趣:25  赞:0   最后回复:2016-01-05 15:14  

SQLite.swift 是一个使用纯 Swift 语言封装 SQLite3 的操作框架。

特性:

  • 简单的查询和参数绑定接口

  • 安全、自动类型数据访问

  • 隐式提交和回滚接口

  • 开发者友好的错误处理和调试

  • 文档完善

  • 通过广泛测试

  • 支持全文检索

  • 支持SQLCipher

示例代码:

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 <- "Alice", email <- "alice@mac.com")
let rowid = try db.run(insert)
// INSERT INTO "users" ("name", "email") VALUES ('Alice', 'alice@mac.com')

for user in try db.prepare(users) {
    print("id: \(user[id]), name: \(user[name]), email: \(user[email])")
    // id: 1, name: Optional("Alice"), email: alice@mac.com
}
// 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)

db.scalar(users.count) // 0
// SELECT count(*) FROM "users"


相关开源代码

您还未登录,请先登录

热门帖子

最新帖子