Dbal 示例代码 介绍
Dbal 是 HuntLabs 开发的D语言(dlang)数据库驱动层,类似 PHP 的
PDO、JAVA 的 JDBC,内置连接池支持。
示例代码
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.
Dbal 示例代码 官网
https://gitee.com/huntlabs/database
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。