Home > php教程 > PHP开发 > body text

dlang operates mysql

高洛峰
Release: 2016-11-22 17:35:24
Original
1495 people have browsed it

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",
	}
}
Copy after login

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;
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!