d2sqlite3 0.8.0

A thin wrapper around SQLite3


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

d2sqlite3

Build Status

This is a small wrapper around SQLite for the D programming language. It wraps the C API in an idiomatic manner and handles built-in D types and Nullable!T automatically.

The SQLite library itself is not included: you have to link your projects to a version of SQLite >= 3.8.7. If you use dub, add the following line to your dub.json file:

    "libs": ["sqlite3"],
    "lflags": ["-L/path/to/lib"]

Synopsis

// Note: exception handling is left aside for clarity.

import std.typecons : Nullable;

// Open a database in memory.
auto db = Database(":memory:");

// Create a table
db.execute(
    "CREATE TABLE person (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        score FLOAT
     )"
);

// Populate the table

// Prepare an INSERT statement
auto statement = db.prepare(
    "INSERT INTO person (name, score)
     VALUES (:name, :score)"
);

// Bind values one by one (by parameter name or index)
statement.bind(":name", "John");
statement.bind(2, 77.5);
statement.execute();

statement.reset(); // Need to reset the statement after execution.

// Bind muliple values at once
statement.bindAll("John", null);
statement.execute();

// Count the changes
assert(db.totalChanges == 2);

// Count the Johns in the table.
auto count = db.execute("SELECT count(*) FROM person WHERE name == 'John'")
               .oneValue!long;
assert(count == 2);

// Read the data from the table lazily
auto results = db.execute("SELECT * FROM person");
foreach (row; results)
{
    // Retrieve "id", which is the column at index 0, and contains an int,
    // e.g. using the peek function (best performance).
    auto id = row.peek!long(0);

    // Retrieve "name", e.g. using opIndex(string), which returns a ColumnData.
    auto name = row["name"].as!string;

    // Retrieve "score", which is at index 2, e.g. using the peek function,
	// using a Nullable type
    auto score = row.peek!(Nullable!double)(2);
	if (!score.isNull) {
		// ...
	}
}

// Read all the table in memory at once
auto data = RowCache(db.execute("SELECT * FROM person"));
foreach (row; data)
{
    auto id = row[0].as!long;
    auto last = row["name"].as!string;
    auto score = row[2].as!(Nullable!double);
    // etc.
}
Authors:
  • Nicolas Sicard
  • Other contributors: see Github repo
Dependencies:
none
System dependencies:
SQLite version >= 3.8.7
Versions:
1.0.0 2021-Jun-10
0.19.1 2020-Jul-21
0.19.0 2020-Jul-21
0.18.3 2019-Aug-06
0.18.2 2019-Jun-25
Show all 46 versions
Download Stats:
  • 15 downloads today

  • 62 downloads this week

  • 219 downloads this month

  • 42080 downloads total

Score:
4.3
Short URL:
d2sqlite3.dub.pm