D语言新手问题 Dlang数据库操作

· Created · Last modified by blue replied at · 333 times read

Dlang 插入数据

import std.stdio;

import hunt.database;

void main()
{
    auto db = new Database("mysql://root:123456@localhost:3306/test?charset=utf8mb4");

    int result = db.execute(`INSERT INTO user(username) VALUES("test")`);
    
    // 打印插入的ID
    writeln(result);

    db.close();
}

Dlang 查询数据

import std.stdio;

import hunt.database;

void main()
{
    auto db = new Database("mysql://root:123456@localhost:3306/test?charset=utf8mb4");

    foreach(row; db.query("SELECT * FROM user LIMIT 10"))
    {
        writeln(row["username"]);
    }

    db.close();
}

Login to reply