Giter Site home page Giter Site logo

libdbnative's Introduction

libDbNative

A dead simple and basic module to access MySQL and Sqliet databases using the C API. Row values are returned as strings.

Examples

import dbconnection;
import mysqlcon;

/// Connect to a database. 
auto con = new MySQL ("localhost", 
                      "user-name",
                      "password",
                      "database");
/* 
// Alternative: 
    auto con = new Sqlite ("/path/to/sqlite-database.db");
*/

/// Execute statements, without retrieving the results.
con.execute (`INSERT INTO STUFF( ID, VAL, THING)
                     VALUES (1, 'val', 'thing')`);

/// Retrieve the last inserted ID.
ulong lastID = con.lastInsertId ();

/// Retrieve results as an associative array.
string [string] rowsAA = con.query (`SELECT * FROM STUFF`);

foreach (row; rowsAA) {
    writeln (row ["VAL"], ": ", row ["THING"]);
}

/// Retrieve values as a result set. More efficient memory wise.
ResultSet rowsRS = con.query (`SELECT * FROM STUFF`);

/// Iterates over the ResultSet as for an associative array.
foreach (row; rowsRS) {
    writeln (row ["VAL"], ": ", row ["THING"]);
}

/// Rewind the result set if you want to reuse it.
rowsRS.rewind ();

Compilation

It can be included as a dub dependency.

For MySQL, compile your program using the mysqlclient library:

  • add a libs "mysqlclient" to your dub.sdl file
  • or "libs": ["mysqlclient"] to your dub.json file.

For Sqlite, compile your program using the sqlite3 library:

  • add a libs "sqlite3" to your dub.sdl file
  • or "libs": ["sqlite3"] to your dub.json file.

Character encoding

The MySQL and Sqlite implementation of Connection are templated: they take a string type as template parameter. That way, you can interact with a database using a character set other than utf-8, as long as that caracter set is supported in D.

import dbconnection;
import mysqlcon;

/// Connect to a database, using a iso-8859-1 character set.
auto con = new MySQL!Latin1String (
                        "localhost", 
                        "user-name",
                        "password",
                        "database");
/* 
// Alternative: 
    auto con = new Sqlite!Latin1String ("/path/to/sqlite-database.db");
*/

/// Now, use the con instance as usual.

libdbnative's People

Contributors

remy-j-a-moueza avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.