DatabaseQueue provides a single database connection that serializes all database accesses. It’s the simplest way to access an SQLite database with GRDB.
Overview
DatabaseQueue opens a single connection to an SQLite database. All database operations are executed serially on a private dispatch queue.
DatabaseQueue when you need:
- Simple, serial database access
- A single-connection database
- Testing with in-memory databases
DatabasePool.
Initialization
Opening a Database File
The path to the database file
Optional database configuration
Creating an In-Memory Database
Optional name for a shared in-memory database
Properties
The database configuration
The path to the database file
Reading from the Database
read(_:)
Synchronously reads from the database.A closure that accesses the database
asyncRead(_:)
Asynchronously reads from the database.unsafeRead(_:)
Reads from the database without wrapping in a transaction.Writing to the Database
write(_:)
Synchronously writes to the database in an implicit transaction.A closure that updates the database
If the closure throws an error, the transaction is rolled back and the error is rethrown.
inTransaction(::)
Executes operations in an explicit transaction.The transaction type (DEFERRED, IMMEDIATE, or EXCLUSIVE)
A closure that performs database operations
writeWithoutTransaction(_:)
Executes database operations without an implicit transaction.Async/Await Support
Memory Management
releaseMemory()
Frees as much memory as possible.Database Interruption
interrupt()
Cancels any running database statement.The database connection is not closed. Interrupted operations throw a
DatabaseError with code SQLITE_INTERRUPT.Closing the Database
close()
Closes the database connection.Temporary Database Copies
inMemoryCopy(fromPath:configuration:)
Creates an in-memory copy of a database file.temporaryCopy(fromPath:configuration:)
Creates a temporary on-disk copy that is automatically deleted when closed.Related Types
DatabasePool- Multi-connection database with concurrent readsDatabase- Low-level database connectionConfiguration- Database configuration options