Please Help

RudeDatabase™ Open Source C++ Database Library

Version 4.1.0

Tutorials and Examples

List of Tables in a Database

This sample program connects to a mysql server on localhost and lists all tables in the mysql database. You can change the connection parameters in the source code.
  1. Download listtables.cpp (1K)
  2. Build with:
    g++ listtables.cpp -lrudedatabase -olisttables
  3. Run with:
    ./listtables

Table Status Report in a Database

This sample program connects to a mysql server on localhost and shows the name, engine, number of rows, and date-time last updated for all tables in the mysql database. You can change the connection parameters in the source code.
  1. Download tablestatus.cpp (1K)
  2. Build with:
    g++ tablestatus.cpp -lrudedatabase -otablestatus
  3. Run with:
    ./tablestatus

Contexts and Replication

This sample program stores database connection settings in a config file (using rudeConfig). It defines one master context, and 3 read-only contexts that connect to replication slaves. When the application performs a read-only query, it uses one of the read-only contexts. When the application needs to perform an update query, it uses the master context. This allows the application to perform its own load balancing. Upon examining the dbcontexts.ini file provided in the example, it should be clear that you can use the same connection parameters for each context - in the event that you only have one server. This allows you to program your application with load-balancing in mind, but allowing you to implement it any way you want using the config file.

This example also demonstrates a very important feature of rudeDatabase: On-demand connection and application-wide persistance:

  • Each defined context will connect the first time it is accessed with Database::instance()
  • An opened context will remain open (connected) and available to your application until you call Database::finish()
  • If a context is never accessed, it will never connect
  1. Download loadcontexts.cpp (2K)
  2. Download dbcontexts.ini (1K)
  3. Build with:
    g++ -oloadcontexts loadcontexts.cpp -lrudeconfig -lrudedatabase
  4. Run with:
    ./loadcontexts