Opens a database connection.
Gets the path associated with an attached database.
Convenience functions equivalent to an SQL statement.
Gets the number of database rows that were changed, inserted or deleted by the most recently executed SQL statement.
Explicitly closes the database.
Convenience functions equivalent to an SQL statement.
Creates and registers a new aggregate function in the database.
Creates and registers a collation function in the database.
Creates and registers a new function in the database.
Enables or disables loading extensions.
Gets the SQLite error code of the last operation.
Gets the SQLite internal handle of the database connection.
Gets the read-only status of an attached database.
Returns the rowid of the last INSERT statement.
Loads an extension.
Prepares (compiles) a single SQL statement and returngs it, so that it can be bound to values before execution.
Convenience functions equivalent to an SQL statement.
Runs an SQL script that can contain multiple statements.
Registers a delegate as the database's commit or rollback hook. Any previously set hook is released.
Sets the progress handler. Any previously set handler is released.
Registers a delegate as the database's commit or rollback hook. Any previously set hook is released.
Registers a delegate as the database's update hook. Any previously set hook is released.
Gets metadata for a specific table column of an attached database.
Gets the number of database rows that were changed, inserted or deleted since the database was opened.
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 }
An SQLite database connection.
This struct is a reference-counted wrapper around a sqlite3* pointer.