search
Homephp教程PHP开发dlang operates mysql

dlang operates mysql

Nov 22, 2016 pm 05:35 PM
mysql

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;
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment