https://github.com/paxa/mysql.d
mysql library binding for D programming language
https://github.com/paxa/mysql.d
Last synced: 9 months ago
JSON representation
mysql library binding for D programming language
- Host: GitHub
- URL: https://github.com/paxa/mysql.d
- Owner: Paxa
- Created: 2014-06-17T05:29:42.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2019-07-27T17:50:14.000Z (almost 7 years ago)
- Last Synced: 2025-01-28T15:16:20.726Z (over 1 year ago)
- Language: D
- Size: 63.5 KB
- Stars: 18
- Watchers: 5
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
Awesome Lists containing this project
README
# Mysql.d
### mysql library binding. Extraction from https://github.com/adamdruppe/arsd
[](https://travis-ci.org/Paxa/mysql.d)
Documentation is not ready yet
```D
import std.stdio;
import mysql.d;
void main() {
auto mysql = new Mysql("localhost", 3306, "root", "root", "mysql_d_testing");
mysql.query("DROP TABLE IF EXISTS users");
mysql.query("CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
sex tinyint(1) DEFAULT NULL,
birthdate DATE,
PRIMARY KEY (id)
);");
mysql.query("insert into users (name, sex, birthdate) values (?, ?, ?);", "Paul", 1, "1981-05-06");
mysql.query("insert into users (name, sex, birthdate) values (?, ?, ?);", "Anna", 0, "1983-02-13");
auto rows = mysql.query("select * from users");
rows.length; // => 2
foreach (user; rows) {
writefln("User %s, %s, born on %s", user["name"], user["sex"] == "1" ? "male" : "female", user["birthdate"]);
}
bool result = mysql.exec("select name, gender from users");
if (!result) {
writefln("SQL Error: %s", mysql.dbErrorMsg);
}
}
```
Output is:
```
User Paul, male, born on 1981-05-06
User Anna, female, born on 1983-02-13
```
**Escaping**
? - convert to string with escaped quotes to prevent sql injection
`?` - convert to string but without quotes
```d
mysql.query("DROP TABLE ?", "table_name"); // error
// SQL error near near ''table_name'' at line 1 :::: DROP TABLE 'table_name'
// should be `some_table` not 'some_table'
mysql.query("DROP TABLE `?`", "table_name"); // working
```
#### Compiling
* Install dub with `brew install dub` or from here http://code.dlang.org/download
* Run `dub`
#### Testing
* run `dub test --config=test`
#### Example application
[http://mysql-d.tk/](http://mysql-d.tk/)