Sql -> Any tuto ?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Aug 8 11:32:42 PDT 2013


On Thu, Aug 08, 2013 at 08:28:21PM +0200, Gary Willoughby wrote:
> On Thursday, 8 August 2013 at 11:18:16 UTC, Larry wrote:
> >https://github.com/rejectedsoftware/mysql-native/blob/master/README.md
> >
> >the native D mysql driver.
> >
> >But then, what to do with it ?
> 
> I've used this quite a bit and works quite nicely. I've found a few
> bugs while using it and they have been promptly fixed, so if you
> find any bugs just file an issue on github. With that said here's
> how i use it:
> 
> Inserting:
> 
> 	auto connection = new Connection("host", "user", "password",
> "database");
> 	auto command    = Command(connection);
> 	command.sql     = "INSERT IGNORE INTO rule (id, severity,
> description) VALUES (?, ?, ?)";
> 	command.prepare();
> 
> 	ulong rowsAffected;
> 
> 	foreach (Rule rule; rules)
> 	{
> 		command.bindParameter(rule.id, 0);
> 		command.bindParameter(cast(ubyte)rule.severity, 1);
> 		command.bindParameter(rule.description, 2);
> 		command.execPrepared(rowsAffected);
> 	}
> 
> 	command.releaseStatement();
> 
> It's quite simple really but there are a few things to remember.
> First *always* call command.releaseStatement(); when you have
> finished with a prepared statement, it leaks memory if you don't.

In that case, it should be written like this:

	...
	command.prepare();
	scope(exit) command.releaseStatement();

	ulong rowsAffected;
	foreach ...

This is exactly the kind of situation scope guards are designed for. So
use them! :-)


T

-- 
If a person can't communicate, the very least he could do is to shut up. -- Tom Lehrer, on people who bemoan their communication woes with their loved ones.


More information about the Digitalmars-d-learn mailing list