/tinysqlite

A lightweight wrapper for SQLite written in Swift

Primary LanguageSwiftMIT LicenseMIT

[![CI Status](http://img.shields.io/travis/Øyvind Grimnes/TinySQLite.svg?style=flat)](https://travis-ci.org/Øyvind Grimnes/TinySQLite) Version License Platform

![alt text] (http://i.imgur.com/KvtukKk.png "Logo")

A lightweight SQLite wrapper written in Swift

###Features

  • Lightweight
  • Object oriented
  • Automatic parameter binding
  • Named parameter binding
  • Thread safe
  • Supports all native Swift types

####Valid datatypes

  • String
  • Character
  • Bool
  • All integer types (signed and unsigned)
  • Float
  • Double
  • NSString
  • NSData
  • NSDate/Date (represented as UNIX timestamps)
  • NSNumber

Usage

Creating the database object

Provide the path to an existing or new database

let databaseQueue = DatabaseQueue(path: path)

Creating queries

All valid SQLite queries are accepted by TinySQLite

To use automatic binding, replace the values in the statement by '?', and provide the values in an array

let query = "INSERT INTO YourTable (column, otherColumn) VALUES (?, ?)"
let values = [1, "A value"]

To use automatic named binding, replace the values in the statement by ':<name>', and provide the values in a dictionary

let query = "INSERT INTO YourTable (column, otherColumn) VALUES (:column, :otherColumn)"
let namedValues = ["column": 1, "otherColumn": "A value"]

Executing updates

Execute an update in the database

try databaseQueue.database { (database) in
    let statement = try database.prepare(query)
    statement.executeUpdate(values)
    statement.executeUpdate(namedValues)
    statemen.finalize()
}

Executing queries

Execute a query to the database.

try databaseQueue.database { (database) in
    let statement = try database.prepare(query)
                                .execute()
    
    for row in statement {
        row.integerForColumn(2) // Returns an integer from the second column in the row
        row.dateForColumn("deadline") // Returns a date from the column called 'deadline'
        row.dictionary // Returns a dictionary representing the row
    }
    
    statement.finalize()
}

Transactions

To improve performance, and prevent partial updates when executing multiple queries, you can use DatabaseQueue's transaction method. If an error is thrown in the block, all changes are rolled back.

try databaseQueue.transaction { (database) in
    try database.prepare(query)
                .executeUpdate()
                .executeUpdate()
                .executeUpdate()
                .finalize()
}

Installation

TinySQLite is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "TinySQLite"

Author

Øyvind Grimnes, oyvindkg@yahoo.com

License

TinySQLite is available under the MIT license. See the LICENSE file for more info.