D 语言数据库驱动层 Dbal

Apache-2.0
D语言 查看源码»
跨平台
2018-05-14
冰力

Dbal 是 HuntLabs 开发的D语言(dlang)数据库驱动层,类似 PHP 的 PDO、JAVA 的 JDBC,内置连接池支持。

目前支持的数据库驱动:

  1. PostgreSQL 8+

  2. MySQL 5.1+

  3. SQLite 3+

示例代码

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

import database;

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

    auto 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

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
  • 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.setParameter(param, value) : bind param's value to :param for sql.

   stmt.setParameter("username", "viile");
   stmt.setParameter("age", 18);
  • ResultSet Statement.query() Return ResultSet

ResultSet 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.

加载中

评论(0)

暂无评论

暂无资讯

暂无问答

Workaround for "1701 Cannot truncate a table referenced in a foreign key constraint" using doctrine:

I've been following a BDD approach using Behat to develop a RESTful API on my current project and wanted to fully isolate the data base from feature to feature, so first I tried...

2016/11/13 20:15
8
0
doctrine 报错处理办法: Unknown database type enum requested

php app/console doctrine:schema:update --force 在sf2update schema的时候 会出现一种报错 数据库不支持枚举类型 D:\htdocs\Symfony>php app/console doctrine:schema:update --force [Do...

2013/11/15 22:21
1K
0
LSP 用户端API 一 开篇

新建 Laravel 项目,并同步git $ valet restart $ cd ~/Sites $ laravel new Lsp-api-user 访问 http://Lsp-api-user.test/,新建成功。 $ cd ~/Sites/Lsp-api-user $ git init $ git check...

11/12 10:41
4
0
Phpstorm 安装 laravel扩展,laravel代码提示插件

Phpstorm 安装 laravel扩展,laravel代码提示插件

2016/03/25 16:04
631
1
Create a restful application with AngularJS and Zend 2 framework (2)

#Replace the backend with Doctrine ORM [Doctrine](http://www.doctrine-project.org) is a very popular data access solution for PHP, it includes low level DBAL and high level OR m...

2013/11/19 17:06
65
0
LSP 商户端API

初始化 Your domain: lsp-api-merchant.hhs2717.cn Virtualhost conf: /usr/local/nginx/conf/vhost/lsp-api-merchant.hhs2717.cn.conf Directory of: ......

11/16 08:59
6
0
Using Redis for Doctrine Caching in Symfony2

Redis is an open source key-value cache and storage, which designed to be very fast since it uses in-memory datasets. Of course, you can persist your data by dumping it to the d...

2015/11/01 18:21
637
0
20个实用的PHP库(图表、图片、数据等)

PHP开发技术在这几年依然比较火热,也有越来越多的开发者加入到了PHP开发阵营,在复杂的框架和冗余的代码面前,选择合适的PHP库就显得 尤为重要,优秀的PHP库可以为你节省很多代码和编码时间...

2014/06/09 12:16
203
0
php 常用类总汇

php 常用类总汇

2015/08/17 16:37
9
0

没有更多内容

加载失败,请刷新页面

返回顶部
顶部