純 Swift 封裝的 SQLite 框架:SQLite.swift
SQLite.swift 是一個使用純 Swift 語言封裝 SQLite3 的操作框架。
特性:
-
簡單的查詢和參數綁定接口
-
安全、自動類型數據訪問
-
隱式提交和回滾接口
-
開發者友好的錯誤處理和調試
-
文檔完善
-
通過廣泛測試

import SQLite
let db = Database("path/to/db.sqlite3")
db.execute(
"CREATE TABLE users (" +
"id INTEGER PRIMARY KEY, " +
"email TEXT NOT NULL UNIQUE, " +
"manager_id INTEGER, " +
"FOREIGN KEY(manager_id) REFERENCES users(id)" +
")"
)
let stmt = db.prepare("INSERT INTO users (email) VALUES (?)")
for email in ["alice@example.com", "betsy@example.com"] {
stmt.run(email)
}
db.totalChanges // 2
db.lastChanges // {Some 1}
db.lastID // {Some 2}
for row in db.prepare("SELECT id, email FROM users") {
println(row)
// [Optional(1), Optional("betsy@example.com")]
// [Optional(2), Optional("alice@example.com")]
}
db.scalar("SELECT count(*) FROM users") // {Some 2}
let jr = db.prepare("INSERT INTO users (email, manager_id) VALUES (? ?)")
db.transaction(
stmt.run("dolly@example.com"),
jr.run("emery@example.com", db.lastID)
) 安裝
Note: SQLite.swift requires Swift 1.1 (available in Xcode 6.1).
To install SQLite.swift:
-
Drag the SQLite.xcodeproj file into your own project. (Submodule, clone, or download the project first.)
-
In your target’s Build Phases, add SQLite iOS (or SQLite Mac) to the Target Dependencies build phase.
-
Add the appropriate SQLite.framework product to theLink Binary With Libraries build phase.
-
Add the same SQLite.framework to a Copy Files build phase with aFrameworks destination. (Add a new build phase if need be.)
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
