db/sqlite
An SQLite database in one file or in memory.
import db.sqlite
SQLite is a database that fits in one file and needs no server; for desktop programs, tools and a great many websites it is enough. The sqlite3 library has to be on the system to run it.
There are two ways to work: the functions of this package, such as Query and Next, which reach everything SQLite offers; or the database and row methods and Conn, which is the shared interface of db. Send user values through Prepare and the Bind... functions, not by pasting them into the SQL text.
Types
Database
struct Database| Name | Type | Description |
|---|---|---|
handle | void* | The SQLite pointer. |
import db
import db.sqlite
func main:
d := sqlite.Open(":memory:")
println d.Kind(), d.Ok(), d.Ping()
_ := d.Exec("CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT, stars REAL)")
_ := d.Exec("INSERT INTO notes (body, stars) VALUES (" + d.Quote("the 'first' note") + ", 4.5)")
println d.LastInsertId(), d.Changes()
tx := d.Begin()
_ := tx.Exec("INSERT INTO notes (body) VALUES ('second')")
txrows := tx.Query("SELECT COUNT(*) FROM notes")
_ := txrows.Next()
println txrows.Int(0)
_ := txrows.Close()
println tx.Ok(), tx.Commit()
rows := d.Query("SELECT id, body, stars FROM notes ORDER BY id")
println rows.Ok(), rows.ColumnCount(), rows.ColumnName(2)
until rows.Next():
mut stars := "-"
if not rows.IsNull(2):
stars = "" + rows.Float(2)
end
println rows.Int64(0), rows.Text(1), stars, rows.ColumnType(2)
end
_ := rows.Close()
tx2 := d.Begin()
_ := tx2.Exec("DELETE FROM notes")
_ := tx2.Rollback()
c := sqlite.Conn(d)
println db.QueryInt(c, "SELECT COUNT(*) FROM notes")
println d.Exec("DROP TABLE nope") != 0, d.Error()
println d.Close()
endKind
func Kind(): strThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Ping
func Ping(): boolThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Exec
func Exec(sql: str): intThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Query
func Query(sql: str): dyn db.RowsThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Begin
func Begin(): dyn db.TxThe example for this item is shown with the ones above: db-sqlite-Methods.salam
LastInsertId
func LastInsertId(): i64The example for this item is shown with the ones above: db-sqlite-Methods.salam
Changes
func Changes(): i64The example for this item is shown with the ones above: db-sqlite-Methods.salam
Error
func Error(): strThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Quote
func Quote(s: str): strThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Close
func Close(): intThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Rows
struct Rows| Name | Type | Description |
|---|---|---|
handle | void* | The statement pointer. |
The example for this item is shown with the ones above: db-sqlite-Methods.salam
Ok
func Ok(): boolThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Next
func Next(): boolThe example for this item is shown with the ones above: db-sqlite-Methods.salam
ColumnCount
func ColumnCount(): intThe example for this item is shown with the ones above: db-sqlite-Methods.salam
ColumnName
func ColumnName(col: int): strThe example for this item is shown with the ones above: db-sqlite-Methods.salam
ColumnType
func ColumnType(col: int): intTYPE_... constants in db; zero for null.The example for this item is shown with the ones above: db-sqlite-Methods.salam
IsNull
func IsNull(col: int): boolThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Text
func Text(col: int): strThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Int
func Int(col: int): intThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Int64
func Int64(col: int): i64The example for this item is shown with the ones above: db-sqlite-Methods.salam
Float
func Float(col: int): f64The example for this item is shown with the ones above: db-sqlite-Methods.salam
Close
func Close(): intThe example for this item is shown with the ones above: db-sqlite-Methods.salam
Txn
struct Txn| Name | Type | Description |
|---|---|---|
handle | void* | The database pointer. |
import db.sqlite
func main:
println sqlite.Available(), sqlite.Version().len() > 0
d := sqlite.Open(":memory:")
println sqlite.Ok(d)
_ := sqlite.Exec(d, "CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT NOT NULL, pages INTEGER, price REAL, note TEXT)")
_ := sqlite.Exec(d, "INSERT INTO books (title, pages, price) VALUES ('The Little Prince', 96, 4.5)")
println sqlite.LastInsertId(d), sqlite.Changes(d)
stmt := sqlite.Prepare(d, "INSERT INTO books (title, pages, price, note) VALUES (?, ?, ?, ?)")
println sqlite.StmtOk(stmt)
_ := sqlite.BindText(stmt, 1, "The Blind Owl")
_ := sqlite.BindInt(stmt, 2, 120)
_ := sqlite.BindFloat(stmt, 3, 6.25)
_ := sqlite.BindNull(stmt, 4)
_ := sqlite.Next(stmt)
_ := sqlite.Reset(stmt)
_ := sqlite.BindText(stmt, 1, "Savushun")
_ := sqlite.BindInt(stmt, 2, 304)
_ := sqlite.BindFloat(stmt, 3, 9)
_ := sqlite.BindText(stmt, 4, "classic")
_ := sqlite.Next(stmt)
_ := sqlite.Finish(stmt)
println sqlite.TotalChanges(d)
rows := sqlite.Query(d, "SELECT id, title, pages, price, note FROM books ORDER BY id")
println sqlite.ColumnCount(rows), sqlite.ColumnName(rows, 1)
until sqlite.Next(rows):
mut note := "-"
if not sqlite.IsNull(rows, 4):
note = sqlite.Text(rows, 4)
end
println sqlite.Int(rows, 0), sqlite.Text(rows, 1), sqlite.Int64(rows, 2), sqlite.Float(rows, 3), note
end
_ := sqlite.Finish(rows)
kinds := sqlite.Query(d, "SELECT pages, title, price, note FROM books WHERE id = 2")
_ := sqlite.Next(kinds)
println sqlite.ColumnType(kinds, 0), sqlite.ColumnType(kinds, 1), sqlite.ColumnType(kinds, 2), sqlite.ColumnType(kinds, 3)
_ := sqlite.Finish(kinds)
println sqlite.QueryInt(d, "SELECT SUM(pages) FROM books"), sqlite.QueryText(d, "SELECT title FROM books ORDER BY price DESC LIMIT 1")
_ := sqlite.Begin(d)
_ := sqlite.Exec(d, "DELETE FROM books")
_ := sqlite.Rollback(d)
_ := sqlite.Begin(d)
_ := sqlite.Exec(d, "UPDATE books SET price = price * 2")
_ := sqlite.Commit(d)
println sqlite.QueryInt(d, "SELECT COUNT(*) FROM books"), sqlite.QueryText(d, "SELECT price FROM books WHERE id = 1")
println sqlite.Exec(d, "SELECT * FROM missing") != 0
println sqlite.Error(d)
_ := sqlite.Close(d)
endOk
func Ok(): boolThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Exec
func Exec(sql: str): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Query
func Query(sql: str): dyn db.RowsThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Commit
func Commit(): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Rollback
func Rollback(): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Functions
Conn
func Conn(d: Database): dyn db.ConnectionConnection.The example for this item is shown with the ones above: db-sqlite-Basics.salam
Available
func Available(): boolThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Version
func Version(): strThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Open
func Open(path: str): Database:memory: means a temporary database in memory.The example for this item is shown with the ones above: db-sqlite-Basics.salam
Ok
func Ok(db: Database): boolThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Close
func Close(db: Database): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Exec
func Exec(db: Database, sql: str): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Error
func Error(db: Database): strThe example for this item is shown with the ones above: db-sqlite-Basics.salam
LastInsertId
func LastInsertId(db: Database): i64The example for this item is shown with the ones above: db-sqlite-Basics.salam
Changes
func Changes(db: Database): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
TotalChanges
func TotalChanges(db: Database): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Begin
func Begin(db: Database): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Commit
func Commit(db: Database): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Rollback
func Rollback(db: Database): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Prepare
func Prepare(db: Database, sql: str): Rows? parameter places; give the values with the Bind... functions. The safest way for user values.The example for this item is shown with the ones above: db-sqlite-Basics.salam
Query
func Query(db: Database, sql: str): RowsNext and call Finish at the end.The example for this item is shown with the ones above: db-sqlite-Basics.salam
StmtOk
func StmtOk(rows: Rows): boolThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Next
func Next(rows: Rows): boolThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Reset
func Reset(rows: Rows): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Finish
func Finish(rows: Rows): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
BindText
func BindText(rows: Rows, idx: int, val: str): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
BindInt
func BindInt(rows: Rows, idx: int, val: i64): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
BindFloat
func BindFloat(rows: Rows, idx: int, val: f64): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
BindNull
func BindNull(rows: Rows, idx: int): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
ColumnCount
func ColumnCount(rows: Rows): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Text
func Text(rows: Rows, col: int): strThe example for this item is shown with the ones above: db-sqlite-Basics.salam
ColumnName
func ColumnName(rows: Rows, col: int): strThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Int
func Int(rows: Rows, col: int): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
Int64
func Int64(rows: Rows, col: int): i64The example for this item is shown with the ones above: db-sqlite-Basics.salam
Float
func Float(rows: Rows, col: int): f64The example for this item is shown with the ones above: db-sqlite-Basics.salam
ColumnType
func ColumnType(rows: Rows, col: int): intColumnType method of the rows returns the shared constants instead.The example for this item is shown with the ones above: db-sqlite-Basics.salam
IsNull
func IsNull(rows: Rows, col: int): boolThe example for this item is shown with the ones above: db-sqlite-Basics.salam
QueryInt
func QueryInt(db: Database, sql: str): intThe example for this item is shown with the ones above: db-sqlite-Basics.salam
QueryText
func QueryText(db: Database, sql: str): strThe example for this item is shown with the ones above: db-sqlite-Basics.salam