Home

Awesome

Build Status

Database

Database abstraction layer for D programing language, support PostgreSQL / MySQL.

Example


import std.stdio;

import hunt.database;

void main()
{
    writeln("run database MySQL demo.");

    auto db = new Database("mysql://root:123456@localhost:3306/test?charset=utf8mb4");

    int result = db.execute(`INSERT INTO user(username) VALUES("test")`);
    writeln(result);

    foreach(row; db.query("SELECT * FROM user LIMIT 10"))
    {
        writeln(row["username"]);
    }

    db.close();
}

Use DatabaseOption to instantiate a Database object

auto options = new DatabaseOption("mysql://root:123456@localhost:3306/test");
options.setMaximumConnection(5);

auto db = new Database(options);

db.execute("SET NAMES utf8");

API

int result = db.execute('INSERT INTO user(username) VALUES("Brian")');
// if execute error ,db will throw an DatabaseException
RowSet rs = db.query("SELECT * FROM user LIMIT 10");
Statement stmt = db.prepare("SELECT * FROM user where username = :username and age = :age LIMIT 10");
stmt.setParameter("username", "viile");
stmt.setParameter("age", 18);
RowSet rs = stmt.query();
foreach(row; rs)
{
    writeln(row["username"]);
}
Row row = stmt.fetch();
writeln(row["username"]);
int result = stmt.execute();

See also