Database

An SQLite database connection.

This struct is a reference-counted wrapper around a sqlite3* pointer.

Constructors

this
this(string path, int flags)

Opens a database connection.

Members

Functions

attachedFilePath
string attachedFilePath(string database)

Gets the path associated with an attached database.

begin
void begin()

Convenience functions equivalent to an SQL statement.

changes
int changes()

Gets the number of database rows that were changed, inserted or deleted by the most recently executed SQL statement.

close
void close()

Explicitly closes the database.

commit
void commit()

Convenience functions equivalent to an SQL statement.

createAggregate
void createAggregate(Deterministic det)

Creates and registers a new aggregate function in the database.

createCollation
void createCollation(T fun)

Creates and registers a collation function in the database.

createFunction
void createFunction(T fun, Deterministic det)

Creates and registers a new function in the database.

enableLoadExtensions
void enableLoadExtensions(bool enable)

Enables or disables loading extensions.

errorCode
int errorCode()

Gets the SQLite error code of the last operation.

execute
ResultRange execute(string sql)

Executes a single SQL statement and returns the results directly. It's the equivalent of prepare(sql).execute().

handle
sqlite3* handle()

Gets the SQLite internal handle of the database connection.

isReadOnly
bool isReadOnly(string database)

Gets the read-only status of an attached database.

lastInsertRowid
long lastInsertRowid()

Returns the rowid of the last INSERT statement.

loadExtension
void loadExtension(string path, string entryPoint)

Loads an extension.

prepare
Statement prepare(string sql)

Prepares (compiles) a single SQL statement and returngs it, so that it can be bound to values before execution.

rollback
void rollback()

Convenience functions equivalent to an SQL statement.

run
void run(string sql, bool delegate(ResultRange) dg)

Runs an SQL script that can contain multiple statements.

setCommitHook
void setCommitHook(int delegate() hook)

Registers a delegate as the database's commit or rollback hook. Any previously set hook is released.

setProgressHandler
void setProgressHandler(int pace, int delegate() handler)

Sets the progress handler. Any previously set handler is released.

setRollbackHook
void setRollbackHook(void delegate() hook)

Registers a delegate as the database's commit or rollback hook. Any previously set hook is released.

setUpdateHook
void setUpdateHook(void delegate(int type, string dbName, string tableName, long rowid) hook)

Registers a delegate as the database's update hook. Any previously set hook is released.

tableColumnMetadata
ColumnMetadata tableColumnMetadata(string table, string column, string database)

Gets metadata for a specific table column of an attached database.

totalChanges
int totalChanges()

Gets the number of database rows that were changed, inserted or deleted since the database was opened.

Examples

1 // Note: exception handling is left aside for clarity.
2 
3 import std.typecons : Nullable;
4 
5 // Open a database in memory.
6 auto db = Database(":memory:");
7 
8 // Create a table
9 db.execute(
10 	"CREATE TABLE person (
11 		id INTEGER PRIMARY KEY,
12 		name TEXT NOT NULL,
13 		score FLOAT
14 	 )"
15 );
16 
17 // Populate the table
18 
19 // Prepare an INSERT statement
20 auto statement = db.prepare(
21 	"INSERT INTO person (name, score)
22 	 VALUES (:name, :score)"
23 );
24 
25 // Bind values one by one (by parameter name or index)
26 statement.bind(":name", "John");
27 statement.bind(2, 77.5);
28 statement.execute();
29 
30 statement.reset(); // Need to reset the statement after execution.
31 
32 // Bind muliple values at once
33 statement.bindAll("John", null);
34 statement.execute();
35 
36 // Count the changes
37 assert(db.totalChanges == 2);
38 
39 // Count the Johns in the table.
40 auto count = db.execute("SELECT count(*) FROM person WHERE name == 'John'")
41 			   .oneValue!long;
42 assert(count == 2);
43 
44 // Read the data from the table lazily
45 auto results = db.execute("SELECT * FROM person");
46 foreach (row; results)
47 {
48 	// Retrieve "id", which is the column at index 0, and contains an int,
49 	// e.g. using the peek function (best performance).
50 	auto id = row.peek!long(0);
51 
52 	// Retrieve "name", e.g. using opIndex(string), which returns a ColumnData.
53 	auto name = row["name"].as!string;
54 
55 	// Retrieve "score", which is at index 3, e.g. using the peek function,
56 	// using a Nullable type
57 	auto score = row.peek!(Nullable!double)(3);
58 	if (!score.isNull) {
59 		// ...
60 	}
61 }
62 
63 // Read all the table in memory at once
64 auto data = RowCache(db.execute("SELECT * FROM person"));
65 foreach (row; data)
66 {
67 	auto id = row[0].as!long;
68 	auto last = row["name"].as!string;
69 	auto score = row[2].as!(Nullable!double);
70 	// etc.
71 }

Meta