C++ and MySQL are both very powerful, but when combined they can make a killer application.
One of the most powerful combinations that any programmer can use is the combination of C++ and MySQL – a flexible programming language with a multi-platform and stable database; but this may seem an intimidating task to the new software developer.
It’s not. This article will show just how easy it is for a programmer to use C++ to:
- set up a connection to a MySQL database
- use the C++ code to access an MySQL stored function
- display the results returned by the MySQL stored function
- and (perhaps most importantly) handle any errors
Setting up Test Data in a MySQL Database
Before a programmer can use a database that database must, of course, exist; or, at very least, a test database must exist. Fortunately creating a database in MySQL is very simple and consists of three steps:
- log on to MySQL
- use SQL to create the MySQL database and any tables
- populate the tables with appropriate data
The first step (logging on to MySQL) can be done from the command line:
Next, simple SQL can be used to the database and tables for the database:
With this done, it’s time to start thinking about doing some actual programming.
Creating a Stored Procedure in a MySQL Database
One of the new additions to MySQL is one that Oracle users will already know – the stored function. The great advantage to using stored functions is that programming code can be built into the database rather than into an application – meaning that multiple applications can use the same piece of code:
This code simply returns the number of active users (from the table users).
Loading the MySQL Header File into C++
When using MySQL with C++ the programmer needs to know absolutely nothing about the actual mechanics of the process – all the programmer has to do is to load the MySQL header file:
C++ Code for Connecting to a Database
This example code above will compile and run, but doesn’t actually do anything – first the C++ code must make a connection to the MySQL database:
The above code:
- initialises the MySQL connection
- makes the connection to the MySQL database (for which the programmer needs to define the host, user name, password and database)
- displays an error message if the connection is rejected for any reason
C++ Code for Running a Query on a MySQL Database
Having made a successful connection to the MySQL database the C++ code may be used to send s SQL query – in this case to run the stored procedure created earlier:
This time the C++ code sends the SQL and then displays another error message if any problem is encountered.
C++ Code for Processing the Results of a MySQL Query
If the connection is successful and the query returns a result (otherwise known as a recordset) then the next step is to display those results:
C++ Code for Disconnecting from a MySQL Database
The final step is to free up any memory used by the recordset and to close the connection:
Compiling and Running the C++ Code
How the code is compiled will depend on the operating system being used and the local set up – in the case of Debian Linux the code would be compiled by using the command:
Assuming, of course, that the code is stored in a file named db.cc.