mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
2e760e41afe6aefb4645ce31110ad94fbbd74479
GRDB
A sqlite3 wrapper for Swift 2.
Goals:
- RAII
- Leverage the Swift standard library (errors, generators, etc.)
- Foster SQL
- Focus on rows, not on tables.
Inspirations:
Usage (work in progress):
let dbQueue = try DatabaseQueue(path: "/tmp/GRDB.sqlite")
try dbQueue.inTransaction { db -> Void in
try db.execute("DROP TABLE IF EXISTS persons")
try db.execute(
"CREATE TABLE persons (" +
"id INTEGER PRIMARY KEY, " +
"name TEXT, " +
"age INT)")
try db.execute(
"INSERT INTO persons (name, age) VALUES (?, ?)",
bindings: ["Arthur", 36])
try db.execute(
"INSERT INTO persons (name, age) VALUES (:name, :age)",
bindings: [":name": "Barbara", ":age": 37])
return .Commit
}
try dbQueue.inDatabase { db -> Void in
for row in try db.fetchRows("SELECT * FROM persons") {
// Leverage Swift type inference
let name: String? = row.value(atIndex: 1)
// Force unwrap when column is NOT NULL
let id: Int64 = row.value(named: "id")!
// Both Int and Int64 are supported
let age: Int? = row.value(named: "age")
print("id: \(id), name: \(name), age: \(age)")
}
// Value sequences require explicit `type` parameter
for name in try db.fetchValues("SELECT name FROM persons", type: String.self) {
// name is `String?` because some rows may have a NULL name.
print(name)
}
}
// names is [String]: ["Arthur", "Barbara"]
let names = try dbQueue.inDatabase { db in
try db.fetchValues("SELECT name FROM persons", type: String.self).map { $0! }
}
Languages
Swift
99.4%
Ruby
0.3%
Makefile
0.2%
C
0.1%