Skip to content

Alexander-Ignition/SQLyra

Repository files navigation

🌌 SQLyra 🎼

Test Swift 5.9 GitHub license

Swift SQLite wrapper.

Documentation

this readme file is available as Xcode playground in Playgrounds/README.playground

Open

Create database in memory for reading and writing.

import SQLyra

let database = try Database.open(
    at: "new.db",
    options: [.readwrite, .memory]
)

Create table

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

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

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)

DataFrame

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     │
└───┴───────┴──────────┘

License

MIT