To enable encryption using SQLCipher, specify the SQLCipher trait.
Note for Xcode users: As of Xcode 26.2 (17C52), you cannot select traits directly in the Xcode UI. You must create a local wrapper package (e.g., AppDependencies) to pull in the dependency with the SQLCipher trait enabled. Ensure you include linkerSettings to link the SQLCipher framework first if other dependencies link standard sqlite3 to prevent conflicts.
Once configured, you can use Connection methods to manage keys:
db.key("secret"): Sets the encryption key.db.rekey("new secret"): Changes the key on an existing encrypted database.db.sqlcipher_export(...): Encrypts an existing unencrypted database.
// In your local wrapper package's Package.swift
// swift-tools-version: 6.1
import PackageDescription
let package = Package(
name: "AppDependencies",
// ... platforms and products ...
dependencies: [
.package(
url: "https://github.com/stephencelis/SQLite.swift.git",
from: "0.16.0",
traits: ["SQLCipher"])
],
targets: [
.target(
name: "AppDependencies",
dependencies: [
.product(name: "SQLite", package: "SQLite.swift")
],
linkerSettings: [
.linkedFramework("SQLCipher")
]
)
]
)
// Usage in your app
import SQLite
let db = try Connection("path/to/encrypted.sqlite3")
try db.key("secret")
try db.rekey("new secret")
// To encrypt an existing database
let db = try Connection("path/to/unencrypted.sqlite3")
try db.sqlcipher_export(.uri("encrypted.sqlite3"), key: "secret")