Import std.db.database
Database
class
Public Class Database
A connection to an SQLite database, which closes when Close is called, or when its last reference goes, and those of its Statements and Transactions. One thread uses it at a time.
Examples
Local db = Try Database.Open(":memory:")
Try db.Execute("CREATE TABLE people (name TEXT, age INTEGER)")
Try db.Execute("INSERT INTO people VALUES (?, ?)", "Ada", 36)
Try db.Execute("INSERT INTO people VALUES (?, ?)", "Alan", 41)
Local rows = Try db.Rows("SELECT name FROM people WHERE age > ?", 40)
Expect(rows.Count == 1 && rows[0].Text("name") == "Alan")
db.Close()-
The database at
path, made when there's none: a file, ":memory:" for a new one in memory that only this Database reaches, or a URI file name, like "file:data.db?mode=ro" to only read one, or "file:name?mode=memory&cache=shared" for one in memory that each Database of that name shares while one is open. Its foreign keys are checked, and it waits up to 5 seconds for another connection that holds the database before it throws a DatabaseError of BUSY. -
Closes it now, and rolls back what an open Transaction did, as its last reference going would, and finalizes its Statements, so it lets go of its file and locks then. Using it, or them, after that panics, but for Close, which does nothing then.
-
Whether Close has closed it.
-
How many rows the last INSERT, UPDATE, or DELETE changed.
-
The rowid of the last row an INSERT added, or 0 before one has.
-
Runs each statement in
sql, in turn, and throws what the first that fails fails with, leaving what the ones before it did, but for what a Transaction undoes. Panics when one takes parameters, which Execute(sql, values) binds. -
Runs
sql's one statement with its parameters bound to values, in order, a value for each, as a Statement's Execute does. Throws a DatabaseError of QUERY whensqlisn't one statement. -
Runs
sql's one statement with its parameters bound toathroughb, in order, as the one that takesaalone does. -
@PanicsAtCaller Public Method Execute<A, B, C>(sql: String, a: A, b: B, c: C) Throws DatabaseErrormethod #Runs
sql's one statement with its parameters bound toathroughc, in order, as the one that takesaalone does. -
@PanicsAtCaller Public Method Execute<A, B, C, D>(sql: String, a: A, b: B, c: C, d: D) Throws DatabaseErrormethod #Runs
sql's one statement with its parameters bound toathroughd, in order, as the one that takesaalone does. -
@PanicsAtCaller Public Method Execute<A, B, C, D, E>(sql: String, a: A, b: B, c: C, d: D, e: E) Throws DatabaseErrormethod #Runs
sql's one statement with its parameters bound toathroughe, in order, as the one that takesaalone does. -
@PanicsAtCaller Public Method Execute<A, B, C, D, E, F>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F) Throws DatabaseErrormethod #Runs
sql's one statement with its parameters bound toathroughf, in order, as the one that takesaalone does. -
@PanicsAtCaller Public Method Execute<A, B, C, D, E, F, G>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F, g: G) Throws DatabaseErrormethod #Runs
sql's one statement with its parameters bound toathroughg, in order, as the one that takesaalone does. -
@PanicsAtCaller Public Method Execute<A, B, C, D, E, F, G, H>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H) Throws DatabaseErrormethod #Runs
sql's one statement with its parameters bound toathroughh, in order, as the one that takesaalone does. -
The rows
sql's one query reads, with its parameters bound to values as Execute's are, so Rows(sql) panics when it takes any. -
The rows
sql's one query reads, each a T, as a Statement'sQuery<T>()gives them. Panics whensqltakes parameters, which a Statement binds.Examples
Struct Person Field Name: String Field Age: Int End Struct Local db = Try Database.Open(":memory:") Try db.Execute("CREATE TABLE people (Name TEXT, Age INTEGER)") Try db.Execute("INSERT INTO people VALUES ('Ada', 36), ('Alan', 41)") Local people = Try db.Query<Person>("SELECT Name, Age FROM people ORDER BY Age") Expect(people.Count == 2 && people[0].Name == "Ada" && people[1].Age == 41) -
@PanicsAtCaller Public Method Query<T, A: SqlValue>(sql: String, a: A): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound to values as Execute's are. -
@PanicsAtCaller Public Method Query<T, A, B>(sql: String, a: A, b: B): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound toathroughb, in order. -
@PanicsAtCaller Public Method Query<T, A, B, C>(sql: String, a: A, b: B, c: C): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound toathroughc, in order. -
@PanicsAtCaller Public Method Query<T, A, B, C, D>(sql: String, a: A, b: B, c: C, d: D): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound toathroughd, in order. -
@PanicsAtCaller Public Method Query<T, A, B, C, D, E>(sql: String, a: A, b: B, c: C, d: D, e: E): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound toathroughe, in order. -
@PanicsAtCaller Public Method Query<T, A, B, C, D, E, F>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound toathroughf, in order. -
@PanicsAtCaller Public Method Query<T, A, B, C, D, E, F, G>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F, g: G): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound toathroughg, in order. -
@PanicsAtCaller Public Method Query<T, A, B, C, D, E, F, G, H>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H): T[] Throws DatabaseError, ParseErrormethod #The rows
sql's one query reads as Ts, with its parameters bound toathroughh, in order. -
@PanicsAtCaller Public Method Rows<A: SqlValue>(sql: String, a: A): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toa, in order. -
@PanicsAtCaller Public Method Rows<A, B>(sql: String, a: A, b: B): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toathroughb, in order. -
@PanicsAtCaller Public Method Rows<A, B, C>(sql: String, a: A, b: B, c: C): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toathroughc, in order. -
@PanicsAtCaller Public Method Rows<A, B, C, D>(sql: String, a: A, b: B, c: C, d: D): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toathroughd, in order. -
@PanicsAtCaller Public Method Rows<A, B, C, D, E>(sql: String, a: A, b: B, c: C, d: D, e: E): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toathroughe, in order. -
@PanicsAtCaller Public Method Rows<A, B, C, D, E, F>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toathroughf, in order. -
@PanicsAtCaller Public Method Rows<A, B, C, D, E, F, G>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F, g: G): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toathroughg, in order. -
@PanicsAtCaller Public Method Rows<A, B, C, D, E, F, G, H>(sql: String, a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H): Row[] Throws DatabaseErrormethod #The rows
sql's one query reads, with its parameters bound toathroughh, in order. -
sql's one statement, prepared to run many times. Throws a DatabaseError of QUERY whensqlholds no statement or more than one, or one that doesn't prepare, like one that names a table the database hasn't got.Examples
Local db = Try Database.Open(":memory:") Try db.Execute("CREATE TABLE squares (n INTEGER, square INTEGER)") Local insert = Try db.Prepare("INSERT INTO squares VALUES (?, ?)") For n = 1 To 3 Try insert.Execute(n, n * n) End For Local rows = Try db.Rows("SELECT SUM(square) FROM squares") Expect(rows[0].Integer(0) == 14) -
A new Transaction, which keeps all of what this Database does until its Commit, or undoes it: see Transaction. One begun while another is open is a part of that one. The outermost is SQLite's IMMEDIATE, which waits for other connections to finish writing when it begins.
Examples
Local db = Try Database.Open(":memory:") Try db.Execute("CREATE TABLE log (line TEXT)") Local transaction = Try db.Begin() Try db.Execute("INSERT INTO log VALUES ('kept only on Commit')") Try transaction.Rollback() Expect((Try db.Rows("SELECT * FROM log")).Count == 0) -
Brings the database up to date with
migrations, the SQL that makes each version of it from the one before: the first makes version 1 from an empty database, the second version 2, and so on. The database keeps its version, as PRAGMA user_version, so each migration runs once, in order, in a Transaction of its own with the version it makes, and one that fails leaves the database as the one before it left it, and throws. Throws a DatabaseError too when the database's version is past the last migration's, as a newer program's would be.Examples
Local db = Try Database.Open(":memory:") Local first = "CREATE TABLE users (name TEXT)" Local second = "ALTER TABLE users ADD COLUMN email TEXT" Try db.Migrate(first) // Again, the same database, with one more migration: only the new one runs. Try db.Migrate(first, second) Try db.Execute("INSERT INTO users VALUES (?, ?)", "Ada", "ada@example.com")
Report an issue with this page or with FutureBlitz on GitHub.