digitalmars.D.learn - D and MySQL
- New2D (3/3) Jul 19 2011 I do quite a bit of work with MySQL. I can't seem to find any informati...
- Trass3r (5/5) Jul 19 2011 http://prowiki.org/wiki4d/wiki.cgi?DatabaseBindings
- Adam Ruppe (20/20) Jul 19 2011 I wrapped the libmysql C library in D and use it in a lot
- Trass3r (2/9) Jul 19 2011 I guess access via opDispatch would also be nice to have?!
- Adam Ruppe (20/21) Jul 19 2011 Use mysql.queryDataObject() for that.
- Trass3r (1/3) Jul 19 2011 Damn, we should really have a common project for database stuff.
- Mandeep (6/9) Jul 20 2011 I had tried writing a library similar to JDBC.
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?
Jul 19 2011
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
Jul 19 2011
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.
Jul 19 2011
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?!
Jul 19 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.
Jul 19 2011
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.
Jul 19 2011
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
Jul 20 2011