Latest 0.1.2
License MIT
Platforms ios 8.0
Dependencies FMDB, EVReflection



To run the example project, clone the repo, and run pod install from the Example directory first.


Swift 4.0+


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

pod 'SQLinq'

How to use

Step 1 – Create Modal class

Create your first modal class that inherits ERDBTableBase.

import SQLinq

class Model: ERDBTableBase {
    var id: NSNumber = 0
    var name: String = ""

    override func tableName() -> String {
        return "MyTable"

Step 2 – Context Class

Context is the database reference point. Create a new class that inherits ERContextBase.

import SQLinq
import FMDB

class Context: ERContextBase {
    lazy var table: DBEntityBase<Model> = {
        [unowned self] in
        let table = DBEntityBase<Model>()
        table.context = self.database
        return table

    override init() {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let databasePath = documentsPath.appending("/database.db")
        self.databasePath = databasePath
        self.database = FMDatabase(path: self.databasePath)

Step3 – Migrations

Migrations are changes that you might want during the app versions. 1st migration is required as that will create the database file and tables. Also it assigns the database version, which will be used to compared next migrations.

import SQLinq

class FirstMigration: ERDbMigration {
    override func up() {
        createTable(table: Model.self)

    override func down() {



Step 4 – Database Initialization & First Migration

Initialize the database & Run the first migration.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        let context: Context = Context()
        context.runMigration(migrationClass: FirstMigration.self)
        return true

Now if you run the application it will create the database & tables.

Insert ROW

Now let’s INSERT first row into our table ‘MyTable’.

let context: Context = Context()

var model = Model() = "iPhone X"

model =  context.table.addOrUpdate(entity: model)
print( // prints the newly assigned id for this row.

SELECT All rows

let context: Context = Context()

let rows =

SELECT With Conditions

let context: Context = Context()

let rows = context.table.whereTrue(condition: "id == 1").whereTrue(condition: "name == 'iPhone X'", conditionType: .OR).toArray()


let context: Context = Context()

let rows = context.table.whereTrue(condition: "id == 1").orderBy(fieldName: "name", direction: ERQueryOrderBy.Descending)


let context: Context = Context()

let rows = context.table.take(count: 100).toArray()

First or Default

This code will search for the name ‘iPhone’ and get the first match. If found it will update the name of the phone.

let context: Context = Context()

if let phone = context.table.whereTrue(condition: "name == 'iPhone X'").firstOrDefault() { = "iPhone 10"
    context.table.addOrUpdate(entity: phone)
} else {
    // handle record not found


Irfan Bashir


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

Latest podspec

    "name": "SQLinq",
    "version": "0.1.2",
    "summary": "SQLite wrapper that has syntax like Linq.",
    "description": "TODO: Add long description of the pod here.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "acct=": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.1.2"
    "social_media_url": "",
    "platforms": {
        "ios": "8.0"
    "source_files": "SQLinq/Classes/**/*",
    "dependencies": {
        "FMDB": [],
        "EVReflection": []
    "pushed_with_swift_version": "3.0"

Pin It on Pinterest

Share This