|
||||||
Using a MySQL Database with C++How to Access MySQL Stored Functions from a C++ Program
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:
Setting up Test Data in a MySQL DatabaseBefore 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:
The first step (logging on to MySQL) can be done from the command line: mysql -u<user> -p<password> mysql
Next, simple SQL can be used to the database and tables for the database: create database cpp_data;
use cpp_data;
create table users(id int, fname varchar(25), sname varchar(25), active bool);
insert into users values (1, 'Fred', 'Smith', True);
insert into users values (2, 'Jane', 'Jones', True);
With this done, it's time to start thinking about doing some actual programming. Creating a Stored Procedure in a MySQL DatabaseOne 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: delimiter //
create function user_count () returns int
deterministic
begin
declare c int;
select count(*) into c from users where active = True;
return c;
end
//
delimiter ;
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: #include <iostream>
#include <mysql.h>
using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int main() {
return 0;
}
C++ Code for Connecting to a DatabaseThis 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: mysql_init(&mysql);
//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
connection = mysql_real_connect(&mysql,"localhost","bainm","not_telling","cpp_data",0,0,0);
if (connection == NULL) {
cout << mysql_error(&mysql) << endl;
return 1;
}
The above code:
C++ Code for Running a Query on a MySQL DatabaseHaving 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: query_state = mysql_query(connection, "select user_count()");
if (query_state !=0) {
cout << mysql_error(connection) << endl;
return 1;
}
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 QueryIf the connection is successful and the query returns a result (otherwise known as a recordset) then the next step is to display those results: result = mysql_store_result(connection);
while ( ( row = mysql_fetch_row(result)) != NULL ) {
cout << row[0] << endl;
}
C++ Code for Disconnecting from a MySQL DatabaseThe final step is to free up any memory used by the recordset and to close the connection: mysql_free_result(result);
mysql_close(connection);
Compiling and Running the C++ CodeHow 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: g++ -o db db.cc -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
Assuming, of course, that the code is stored in a file named db.cc. ConclusionBoth the MySQL database and the C++ programming language are powerful tools in their own right; and combined they are an incredibly important tool for the software developer - an important tool and one which is very easy to use, and very, very effective. Further ReadingMySQL Stored Procedures and Functions
The copyright of the article Using a MySQL Database with C++ in C Programming is owned by Mark Alexander Bain. Permission to republish Using a MySQL Database with C++ in print or online must be granted by the author in writing.
Comments
Dec 20, 2008 6:00 PM
Guest :
Feb 22, 2009 9:34 PM
Guest :
Apr 29, 2009 6:01 AM
Guest :
Aug 16, 2009 7:03 AM
Guest :
4 Comments
|
||||||
|
|
||||||
|
|
||||||