Latest 0.4.4
Homepage https://github.com/Oyvindkg/tinysqlite
License MIT
Platforms ios 8.0, requires ARC
Dependencies sqlite3
Authors

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

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

A lightweight SQLite wrapper written in Swift

Features

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

Usage

Creating the database object

Provide the DatabaseQueue with a file URL. If the database file exists, the existing database file will be used. If not, a new database will be created automatically.

let location = URL(fileURLWithPath: "path/to/database.sqlite")

let databaseQueue = DatabaseQueue(location: location)

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 parameters = [1, "A value"]

To use automatic named binding, replace the values in the statement by ‘:‘, and provide the values in a dictionary

let query = "INSERT INTO YourTable (column, otherColumn) VALUES (:column, :otherColumn)"

let parameterMapping = [
    "column": 1, 
    "otherColumn": "A value"
]

Executing updates

Execute an update in the database

try databaseQueue.database { (database) in
    let statement = try database.statement(for: query)

    statement.executeUpdate()
    statement.executeUpdate(withParameters: parameters)
    statement.executeUpdate(withParameterMapping: parameterMapping)

    statement.finalize()
}

Executing queries

Execute a query to the database.

try databaseQueue.database { (database) in
    let statement = try database.statement(for:query)

    try statement.execute()

    for row in statement {

        /* Get an integer from the second column in the row */
        row.integerForColumn(at: 2)

        /* Get a date from the column called 'deadline' */
        row.dateForColumn("deadline") 

        /* Get a dictionary representing the row */
        row.dictionary 
    }

    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.statement(for: query)
                .executeUpdate(withParameters: someParameters)
                .executeUpdate(withParameters: someOtherParameters)
                .executeUpdate(withParameters: evenMoreParameters)
                .finalize()
}

Installation

Cocoapods

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

pod "TinySQLite", "0.4.4"

Carthage

TinySQLite is available through Carthage. To install, simply add the following line to your Cartfile:

github "Oyvindkg/tinysqlite" "0.4.4"

Author

Øyvind Grimnes, [email protected]

License

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

Latest podspec

{
    "name": "TinySQLite",
    "version": "0.4.4",
    "summary": "A lightweight, type safe, SQLite wrapper written in Swift 3",
    "description": "lightweight, type safe, SQLite wrapper written in Swift 3 with error handling",
    "homepage": "https://github.com/Oyvindkg/tinysqlite",
    "license": "MIT",
    "authors": {
        "u00d8yvind Grimnes": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Oyvindkg/tinysqlite.git",
        "tag": "0.4.4"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "TinySQLite/Classes/**/*",
    "dependencies": {
        "sqlite3": []
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This