Database
#struct DatabaseOne open SQLite 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()
endsqlite true true 1 1 2 true 0 true 3 stars 1 the 'first' note 4.5 2 2 second - 0 2 true no such table: nope 0