SwiftSQL introduces a Swift API for SQLite. It doesn't have any ORM-like features. It maps directly to the SQLite concepts with some affordances to make it great a Swift API. It is feature-complete, fully documented and tested.
An instance of SQLStatement
represents a single SQL statement that has been compiled into binary form and is ready to be evaluated.
Think of each SQL statement as a separate computer program. The original SQL text is source code. A prepared statement object is the compiled object code. All SQL must be converted into a prepared statement before it can be run.
The life-cycle of a prepared statement object usually goes like this:
- Create the prepared statement object using a connection:
let db = try SQLConnection(url: storeURL)
let statement = try db.statement("""
INSERT INTO Users (Name, Surname)
VALUES (?, ?)
""")
- Bind values to parameters using one of the
bind()
methods. The provided values must be one of the data types supported by SQLite (seeSQLDataType
for more info)
try statement.bind("John", "Appleseed")
- Execute the statement.
// Using `execute()` method
try statement.execute()
// If it's a `SELECT` query
// See `SQLRow` type for more info how to read data from the columns.
while let row = try statement.next() {
let name: String = row[0]
}
- (Optional) To reuse the compiled statementt, reset it and go back to step 2, do this zero or more times.
try statement.reset()
The compiled statement is going to be automatically destroyed when the
SQLStatement
object gets deallocated.
All of the methods outlined in the previous section are chainable:
try db.statement("INSERT INTO Users (Name) VALUES (?)")
.bind("Alex")
.execute()
SQLDataType
makes it possible to use native Swift
types to work with SQLite. Supported types:
Int
Int32
Int64
String
Data
To add support for custom data types, like
Bool
orDate
, see Advanced Usage Guide
SwiftSQL | Swift | Xcode | Platforms |
---|---|---|---|
SwiftSQL 0.1 | Swift 5.2 | Xcode 11.3 | iOS 11.0 / watchOS 4.0 / macOS 10.13 / tvOS 11.0 |
SwiftSQL is available under the MIT license. See the LICENSE file for more info.