d operates mysql
d has a very convenient tool dub, which needs to write formatted json files
Dub instructions can be found at http://code.dlang.org/package-format?lang=json
my dub Format, save as dub.json
{ "name": "testmysql", "description": "test mysql connect.", "authors": ["cabing_2005@126.com"], "homepage": "http://my.oschina.net/u/218155/blog?catalog=3451757", "license": "GPL-2.0", "dependencies": { "vibe-d": "~>0.7.17", "mysql-native" :"~>0.1.3", } }
I used the connection pool of vibe.d and the database package of mysqlnative, so I put them both in the dependencies.
File structure Create a new directory in the current directory and name it source. Create a new script app.d under source
Finally run the code. Run dub at the upper level of source
The specific code
import mysql.common; import mysql.connection; import mysql.result; import mysql.db; import std.stdio;ulong testExce(Connection cn,string sql){ auto cmd = Command(cn); cmd.sql = sql; ulong rowsAffected; cmd.execSQL(rowsAffected); return rowsAffected; }ResultSet testRows()(Connection cn, string sql){ auto cmd = Command(cn); cmd.sql = sql; return cmd.execSQLResult(); }void testMysql(){ string connStr = "host=localhost;port=3306;user=root;pwd=123456;db=test"; auto mdb = new MysqlDB(connStr); auto con = mdb.lockConnection(); scope(exit) con.close(); //测试增删改查 //add auto addSql = "insert into country(name,user_age,id)values('helloworld',59,1)"; writeln("add data is ", testExce(con,addSql)); //update auto updateSql = "update country set name = 'helloworld' where id=1 limit 1"; writeln("update data is ", testExce(con,updateSql)); //delte auto delSql = "delete from country where id=1 limit 1"; writeln("delete data is ", testExce(con,delSql)); //select auto selSql = "select name,user_age,id from country"; ResultSet rs = testRows(con,selSql); int i; auto keys = ["name","user_age","id"]; for(i=0;i<rs.length;i++){ foreach(k,v;keys){ writef("%s:%s",v,rs[i][k]); } writeln(""); } writeln(rs[0],rs[0][0],rs[0][1]); }int main(char[][] args) { testMysql(); return 0; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment