Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

The Database Component

The Database component provides a mechanism for attaching to a SQL database and executing queries against the database. The queries may be either result generating queries (such as SQL select) or updating queries (SQL update, insert, delete and DDL type queries).

A typical SQL select fragment is

     #include "mcllib/mcllib.h"

     using namespace mcllib;
     using namespace mcllib::db;
     ...
     // connection string specifies a database type (mysql in this case)
     // and parameters to pass to the driver to connect to that database
     MCString connectionString("mcdb:mysql://user=admin;pwd=admin;db=mysql");
     // get a database connection (login to database)
     MCDBConnection conn = MCDBDriver::getConnection(connectionString);
     // get a SQL statement from the connection
     MCSqlStatement stmt = conn.createStatement();
     // construct a SELECT to execute
     MCStringStream str;
     str << "select name, age from people where age > " << ageMin;
     // execute the select - returns true if the query was a SELECT statement
     if (stmt.execute(str.getString())) 
     {
         // got a result
         MCDBResultSet rslt = stmt.getResultSet();
         // getRowCount() shows how many records in the result set
         cout << "Retrieved " << rslt.getRowCount()
              << " rows from table" << endl;
         // iterate over the result set
         while (rslt.next()) {
             // Columns in result set are numbered from 0
             // and stored as variant types
             // extract column 0 as the name (string)
             MCVTString name(rslt.get(0));
             // extract column 1 as the age (int)
             MCVTUInt32 age(rslt.get(1));
             cout << "Name: " << (MCString)name <<
                     ", Age: " << (mcuint32)age << endl;
         }
     } else {
         cout << "Query did not return a result" << endl;
     }
 
Typically, the database connection will be made once for many SQL statements. The execute() and getResultSet() will be called as often as necessary. If the SQL statement is in error, an exception is thrown.
Generated on Wed Jan 12 19:05:48 2005 for MCLLIB by  doxygen 1.3.9.1