Giter VIP home page Giter VIP logo

ddbc's Introduction

DDBC

DDBC is DB Connector for D language (similar to JDBC)

Currently supports MySQL, PostgreSQL and SQLite.

Project homepage: https://github.com/buggins/ddbc Documentation: https://github.com/buggins/ddbc/wiki

See also: https://github.com/buggins/hibernated - ORM for D language which uses DDBC.

NOTE: project has been moved from SourceForge to GitHub

Example:

import ddbc.all;
import std.stdio;
import std.conv;

// Create driver and fill connection params. You can leave only one section - for RDBMS you want to use
string[string] params;
// This part depends on RDBMS
version( USE_SQLITE )
{
    SQLITEDriver driver = new SQLITEDriver();
    string url = "zzz.db"; // file with DB
}
else version( USE_PGSQL )
{
    PGSQLDriver driver = new PGSQLDriver();
    string url = PGSQLDriver.generateUrl( "/tmp", 5432, "testdb" );
    params["user"] = "hdtest";
    params["password"] = "secret";
    params["ssl"] = "true";
} else version(USE_MYSQL)
{
    // MySQL driver - you can use PostgreSQL or SQLite instead as well
    MySQLDriver driver = new MySQLDriver();
    string url = MySQLDriver.generateUrl("localhost", 3306, "test_db");
    params = MySQLDriver.setUserAndPassword("testuser", "testpassword");
}
// create connection pool
DataSource ds = new ConnectionPoolDataSourceImpl(driver, url, params);

// creating Connection
auto conn = ds.getConnection();
scope(exit) conn.close();

// creating Statement
auto stmt = conn.createStatement();
scope(exit) stmt.close();

// execute simple queries to create and fill table
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ddbct1 (id bigint not null primary key AUTO_INCREMENT, name varchar(250), comment mediumtext, ts datetime)");
stmt.executeUpdate("INSERT INTO ddbct1 SET id=1, name='name1', comment='comment for line 1', ts='20130202123025'");
stmt.executeUpdate("INSERT INTO ddbct1 SET id=2, name='name2', comment='comment for line 2 - can be very long'");

// reading DB
auto rs = stmt.executeQuery("SELECT id, name name_alias, comment, ts FROM ddbct1 ORDER BY id");
while (rs.next())
    writeln(to!string(rs.getLong(1)) ~ "\t" ~ rs.getString(2) ~ "\t" ~ strNull(rs.getString(3)));

ddbc's People

Contributors

arjankn avatar buggins avatar s-ludwig avatar

Watchers

 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.