Files
sqlite/example.ar
T
2026-08-01 20:20:58 +01:00

74 lines
1.5 KiB
Plaintext

# SPDX-FileCopyrightText: 2026 William Bell
#
# SPDX-License-Identifier: GPL-3.0-or-later
import "src" expose Sqlite, sql
let db = Sqlite("test.db")
# Create table if it doesn't exist
db.execute(sql`CREATE TABLE IF NOT EXISTS contacts (
contact_id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL,
phone TEXT NOT NULL
);`)
# Re-open cursor for insert
let some_first_name = "will"
let some_last_name = "bell"
let some_email = "william@wbell.dev"
let some_phone = "573403287534893278537592387958723"
# Insert rows
# db.execute(sql`BEGIN TRANSACTION;`)
# for (i in 0 until 1e2) do
# db.execute(sql`
# INSERT INTO contacts (first_name, last_name, email, phone)
# VALUES ($(some_first_name), $(some_last_name), $(some_email), $(i))
# `)
# db.execute(sql`COMMIT;`)
for (row in db.execute(sql`
SELECT *
FROM contacts
WHERE email=$(some_email)
`)) do
term.log(row)
term.log(db.execute(sql`
SELECT *
FROM contacts
WHERE email=$(some_email)
`).many())
term.log(db.execute(sql`
SELECT *
FROM contacts
WHERE email=$(some_email)
`))
term.log(db.execute(sql`
SELECT *
FROM contacts
WHERE email=$(some_email)
`).one())
term.log(db.execute(sql`
SELECT *
FROM contacts
WHERE email=$(some_email)
`).maybe_one())
term.log(db.execute(sql`
SELECT *
FROM contacts
WHERE email=$(some_email)
`).one_first())
term.log(db.execute(sql`
SELECT *
FROM contacts
WHERE email=$(some_email)
`).maybe_one_first())
db.close()