Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
July 19, 2011 D and MySQL | ||||
---|---|---|---|---|
| ||||
I do quite a bit of work with MySQL. I can't seem to find any information on how to use D with MySQL. Is there a tutorial or example around that someone can point me to? |
July 19, 2011 Re: D and MySQL | ||||
---|---|---|---|---|
| ||||
Posted in reply to New2D | http://prowiki.org/wiki4d/wiki.cgi?DatabaseBindings There is some information, but it's probably outdated. Please update that wiki once you know more :) Apart from that I only know of this binding: http://dsource.org/projects/ddbi |
July 19, 2011 Re: D and MySQL | ||||
---|---|---|---|---|
| ||||
Posted in reply to New2D | I wrapped the libmysql C library in D and use it in a lot of my apps. https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff Grab database.d and mysql.d from there. To use it: === import arsd.mysql; void main() { auto mysql = new MySql("localhost", "username", "password", "database name"); // ? based placeholders do conversion and escaping for you foreach(line; mysql.query("select id, name from users where id > ?", 5)) { // access to columns by name writefln("%s: %s", line["id"], line["name"]); // alternatively, you can write: writefln("%s: %s", line[0], line[1]); } } ======= There's a lot of other stuff in there too, which I'll write up in the next week or so... but this is the basics of it. |
July 19, 2011 Re: D and MySQL | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam Ruppe | Am 19.07.2011, 20:49 Uhr, schrieb Adam Ruppe <destructionator@gmail.com>:
> foreach(line; mysql.query("select id, name from users where id > ?", 5)) {
>
> // access to columns by name
> writefln("%s: %s", line["id"], line["name"]);
> // alternatively, you can write:
> writefln("%s: %s", line[0], line[1]);
>
> }
I guess access via opDispatch would also be nice to have?!
|
July 19, 2011 Re: D and MySQL | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | 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.
|
July 19, 2011 Re: D and MySQL | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam Ruppe | > The DataObject is found in database.d - it is meant to work generically with any database backend.
Damn, we should really have a common project for database stuff.
|
July 20, 2011 Re: D and MySQL | ||||
---|---|---|---|---|
| ||||
Posted in reply to New2D | On 07/20/2011 12:04 AM, New2D wrote: > I do quite a bit of work with MySQL. I can't seem to find any information on > how to use D with MySQL. Is there a tutorial or example around that someone > can point me to? I had tried writing a library similar to JDBC. http://dsource.org/projects/ddbc. It doesnt have a native driver for Mysql but the odbc example on the home page was tested using Mysql worked with ddbc 2.052. Mandeep |
Copyright © 1999-2021 by the D Language Foundation