Swift GRDB 入门指南:SQLite 数据库操作
Swift GRDB 是一个用于 iOS 和 macOS 的 SQLite 数据库访问框架。它提供了一个简单易用的 API,可以在 Swift 中进行 SQL 查询和数据操作。下面是一个简单的 Swift GRDB 的使用示例:
- 导入 GRDB
在项目中导入 GRDB:
import GRDB
- 创建数据库连接
在应用程序中创建数据库连接:
let dbQueue = try DatabaseQueue(path: 'path/to/database.sqlite')
- 创建表
在数据库中创建表:
try dbQueue.write { db in
try db.create(table: 'persons') { t in
t.column('id', .integer).primaryKey()
t.column('name', .text)
t.column('age', .integer)
}
}
- 插入数据
插入数据到表中:
try dbQueue.write { db in
try db.execute(
sql: 'INSERT INTO persons (name, age) VALUES (?, ?)',
arguments: ['John', 30])
}
- 查询数据
查询表中的数据:
let persons = try dbQueue.read { db in
try Person.fetchAll(db)
}
以上是 Swift GRDB 的基本使用方法。更多高级用法,请参考 GRDB 的文档。
原文地址: https://www.cveoy.top/t/topic/oRej 著作权归作者所有。请勿转载和采集!