Swift SQLite wrapper.
this readme file is available as Xcode playground in Playgrounds/README.playground
Create database in memory for reading and writing.
import SQLyra
let database = try Database.open(
at: "new.db",
options: [.readwrite, .memory]
)
Create table for contacts with fields id
and name
.
let sql = """
CREATE TABLE contacts(
id INT PRIMARY KEY NOT NULL,
name TEXT
);
"""
try database.execute(sql)
Insert new contacts Paul and John.
let insert = try database.prepare("INSERT INTO contacts (id, name) VALUES (?, ?);")
try insert.bind(parameters: 1, "Paul").execute().reset()
try insert.bind(parameters: 2, "John").execute()
Select all contacts from database.
struct Contact: Codable {
let id: Int
let name: String
}
let select = try database.prepare("SELECT * FROM contacts;")
let contacts = try select.array(Contact.self)
The DataFrame from the TabularData framework is supported.
It can help to print the table.
let df = try database.prepare("SELECT * FROM contacts;").dataFrame()
print(df)
┏━━━┳━━━━━━━┳━━━━━━━━━━┓
┃ ┃ id ┃ name ┃
┃ ┃ <Int> ┃ <String> ┃
┡━━━╇━━━━━━━╇━━━━━━━━━━┩
│ 0 │ 1 │ Paul │
│ 1 │ 2 │ John │
└───┴───────┴──────────┘
MIT