database 0.0.2

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


To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Database

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

Example


import std.stdio;
import std.experimental.logger;

import database;

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

    Database db = new Database("mysql://root:123456@localhost:3306/test?charset=utf-8");

    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

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

Database 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
  • ResultSet Database.query(sql) Return ResultSet object for query(SELECT).
    ResultSet 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.bind(param, value) : bind param's value to :param for sql.
   stmt.bind("username", "viile");
   stmt.bind("age", 18);
  • ResultSet Statement.query() Return ResultSet
    ResultSet rs = stmt.query();
  • 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: none

Versions:
0.0.2 2017-Jun-30
0.0.1 2017-Jun-27
~master 2017-Jul-13
Show all 3 versions
Stats:
  • 0 downloads today

  • 2 downloads this week

  • 7 downloads this month

  • 282 downloads total