Connect with us

Code

GRDB – A toolkit for SQLite databases, with a focus on application development

Use this library to save your application’s permanent data into SQLite databases. It comes with built-in tools that address common needs:

  • SQL Generation

    Enhance your application models with persistence and fetching methods, so that you don’t have to deal with SQL and raw database rows when you don’t want to.

  • Database Observation

    Get notifications when database values are modified.

  • Robust Concurrency

    Multi-threaded applications can efficiently use their databases, including WAL databases that support concurrent reads and writes.

  • Migrations

    Evolve the schema of your database as you ship new versions of your application.

  • Leverage your SQLite skills

    Not all developers need advanced SQLite features. But when you do, GRDB is as sharp as you want it to be. Come with your SQL and SQLite skills, or learn new ones as you go!

GRDB  Usage

import GRDB

// 1. Open a database connection
let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")

// 2. Define the database schema
try dbQueue.write { db in
    try db.create(table: "player") { t in
        t.primaryKey("id", .text)
        t.column("name", .text).notNull()
        t.column("score", .integer).notNull()
    }
}

// 3. Define a record type
struct Player: Codable, FetchableRecord, PersistableRecord {
    var id: String
    var name: String
    var score: Int
}

// 4. Write and read in the database
try dbQueue.write { db in
    try Player(id: "1", name: "Arthur", score: 100).insert(db)
    try Player(id: "2", name: "Barbara", score: 1000).insert(db)
}

let players: [Player] = try dbQueue.read { db in

Documentation

GRDB runs on top of SQLite: you should get familiar with the SQLite FAQ. For general and detailed information, jump to the SQLite Documentation.

Demo Applications & Frequently Asked Questions

  • Demo Applications: Three flavors: vanilla UIKit, Combine + SwiftUI, and Async/Await + SwiftUI.
  • FAQ

Reference

Getting Started

SQLite and SQL

Records and the Query Interface

Application Tools

  • Migrations: Transform your database as your application evolves.
  • Full-Text Search: Perform efficient and customizable full-text searches.
  • Database Observation: Observe database changes and transactions.
  • Encryption: Encrypt your database with SQLCipher.
  • Backup: Dump the content of a database to another.
  • Interrupt a Database: Abort any pending database operation.
  • Sharing a Database: How to share an SQLite database between multiple processes – recommendations for App Group containers, App Extensions, App Sandbox, and file coordination.

Good to Know

FAQ

Sample Code

GRDB on GitHub: https://github.com/groue/GRDB.swift
Platfrom: iOS
⭐️: 6.1K
Advertisement

Trending