D and MySQL

Adam Ruppe destructionator at gmail.com
Tue Jul 19 12:03:20 PDT 2011


Trass3r wrote:
> I guess access via opDispatch would also be nice to have?!

Use mysql.queryDataObject() for that.

foreach(line; mysql.queryDataObject(...rest is the same...) {
    writeln(line.id, line.name);
//    line["id"]
//    line["name"]
}


all work. But with the DataObject, you don't have integer indexes.

You can also foreach(name, value; line) to go over the returned fields, just like
with an associative array.


What you gain though is write support:

line.name = "something else";
line.commitChanges(); // does an update



You can also create new DataObjects:

auto obj = new DataObject(mysql, "users");

obj.id = 10;
obj.name = "My Name";

obj.commitChanges(); // does a select. if empty, inserts. if not, updates.



The DataObject is found in database.d - it is meant to work generically with any
database backend.


More information about the Digitalmars-d-learn mailing list