hunt-database 2.3.0

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


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:

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 Database.execute(string sql) Return number of execute result.
int result = db.execute('INSERT INTO user(username) VALUES("Brian")');
// if execute error ,db will throw an DatabaseException
  • RowSet Database.query(sql) Return RowSet object for query(SELECT).
RowSet rs = db.query("SELECT * FROM user LIMIT 10");
  • Statement Database.prepare(sql) Create a prepared Statement object.
Statement stmt = db.prepare("SELECT * FROM user where username = :username and age = :age LIMIT 10");
  • Statement.setParameter(param, value) : bind param's value to :param for sql.
stmt.setParameter("username", "viile");
stmt.setParameter("age", 18);
  • RowSet Statement.query() Return RowSet
RowSet rs = stmt.query();
foreach(row; rs)
{
    writeln(row["username"]);
}
  • Row Statement.fetch() Return Row
Row row = stmt.fetch();
writeln(row["username"]);
  • int Statement.execute() : return execute status for prepared Statement object.
int result = stmt.execute();
  • Statement.lastInsertId() : Statement.execute() for insert sql, return lastInsertId.
Dependencies:
hunt-net, hunt-sql
Versions:
2.3.7 2023-Nov-15
2.3.6 2022-Dec-06
2.3.5 2022-Jul-22
2.3.4 2022-Jun-02
2.3.3 2022-Feb-21
Show all 31 versions
Download Stats:
  • 0 downloads today

  • 3 downloads this week

  • 11 downloads this month

  • 7735 downloads total

Score:
1.6
Short URL:
hunt-database.dub.pm