www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D and MySQL

reply New2D <new2d what.com> writes:
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
next sibling parent Trass3r <un known.com> writes:
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
prev sibling next sibling parent reply Adam Ruppe <destructionator gmail.com> writes:
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
parent reply Trass3r <un known.com> writes:
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
parent reply Adam Ruppe <destructionator gmail.com> writes:
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
parent Trass3r <un known.com> writes:
 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
prev sibling parent Mandeep <mandeep brars.co.in> writes:
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