Documentation Home
MySQL Connector/C++ Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.6Mb
PDF (A4) - 0.6Mb
HTML Download (TGZ) - 432.4Kb
HTML Download (Zip) - 455.5Kb

MySQL Connector/C++ Developer Guide  /  Connector/C++ Tutorials  /  Prerequisites and Background Information

8.1 Prerequisites and Background Information

This section describes the prerequisites that must be satisifed before you work through the remaining tutorial sections, and shows how to set up the framework code that serves as the basis for the tutorial applications.

These tutorials refer to tables and sample data from the world database, which you can download from the Example Databases section of the MySQL Documentation page.

Each tutorial application uses a framework consisting of the following code. The examples vary at the line that says /* INSERT TUTORIAL CODE HERE! */ within the try block, which is replaced for each application with the application-specific code.

#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
/* uncomment for applications that use vectors */
/*#include <vector>*/

#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "worlduser"
#define EXAMPLE_PASS "worldpass"
#define EXAMPLE_DB "world"

using namespace std;

int main(int argc, const char **argv)
  string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
  const string user(argc >= 3 ? argv[2] : EXAMPLE_USER);
  const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS);
  const string database(argc >= 5 ? argv[4] : EXAMPLE_DB);

  cout << "Connector/C++ tutorial framework..." << endl;
  cout << endl;

  try {


  } catch (sql::SQLException &e) {
      MySQL Connector/C++ throws three different exceptions:

      - sql::MethodNotImplementedException (derived from sql::SQLException)
      - sql::InvalidArgumentException (derived from sql::SQLException)
      - sql::SQLException (derived from std::runtime_error)
    cout << "# ERR: SQLException in " << __FILE__;
    cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
    /* what() (derived from std::runtime_error) fetches error message */
    cout << "# ERR: " << e.what();
    cout << " (MySQL error code: " << e.getErrorCode();
    cout << ", SQLState: " << e.getSQLState() << " )" << endl;

    return EXIT_FAILURE;

  cout << "Done." << endl;
  return EXIT_SUCCESS;

To try the framework code as a standalone program, use this procedure:

  1. Copy and paste the framework code to a file such as framework.cpp. Edit the #define statements to reflect your connection parameters (server, user, password, database). Also, because the file contains those parameters, set its access mode to be readable only to yourself.

  2. Compile the framework. For example, on OS X, the command might look like this (enter the command on one line):

    shell> g++ -o framework
      -I/usr/local/include -I/usr/local/include/cppconn
      framework.cpp -lmysqlcppconn

    Adapt the command as necessary for your system. A similar command is needed for the tutorial applications that follow.

  3. To run the framework, enter the following:

    shell> ./framework

    You will see a simple message:

    Connector/C++ tutorial framework...

You are now ready to continue to the tutorials.

User Comments
  Posted by Garry HURLEY on May 19, 2017
As general comment, if you are going to use this same set of includes in multiple files, I suggest making a header file which wraps all of them. Including a single header file is much better than trying to include multiples. Also, it makes things easier to maintain on your system.
Sign Up Login You must be logged in to post a comment.