jsonstein@masto.deoan.org ("Jeff Sonstein") wrote:
I like how readable it comes out
----- simple example -----
import std.stdio : writeln, writefln;
import d2sqlite3;void main() {
try {
// connect to the db. create 'test.db' if it does not exist
auto db = Database( "test.db" );// create a table if it does not exist
db.execute("
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
score INTEGER NOT NULL
)
");// insert sample data using nice safe prepared statements
auto insertStmt = db.prepare( "INSERT INTO users (name, score) VALUES (:name, :score)");// clear and insert our friend Alice
insertStmt.bind( ":name", "Alice" );
insertStmt.bind( ":score", 82 );
insertStmt.execute();
insertStmt.reset();// clear and insert Bob now
insertStmt.bind( ":name", "Bob" );
insertStmt.bind( ":score", 95 );
insertStmt.execute();// query the database and loop through rows using idiomatic D
writeln( "-- Begin User List --" );
ResultRange allRows = db.execute( "SELECT id, name, score FROM users" );
foreach( Row thisRow; allRows ) {
// extract values using zero-based index or column name string
long id = thisRow[ 0 ].as!long;
string name = thisRow[ "name" ].as!string;
int score = thisRow[ "score" ].as!int;
writefln( "ID: %d | Name: %s | Score: %d", id, name, score );
}
writeln( "-- End User List --" );
}
catch( SqliteException e ) {
// handle db specific exceptions
writefln( "SQLite Error Code: %d", e.code );
writefln( "Error Message: %s", e.msg );
}
catch( Exception f ) {
// catchall for generic weirdness
writefln( "Generic Error Message: %s", f.msg );
}
}