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 {
/* INSERT TUTORIAL CODE HERE! */
} 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:
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.-
Compile the framework. For example, on macOS, the command might look like this (enter the command on one line):
$> 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.
-
To run the framework, enter the following:
$> ./framework
You will see a simple message:
Connector/C++ tutorial framework... Done.
You are now ready to continue to the tutorials.