// you’re reading...

C / C++ / Objective-C

ANSI C++ MySQL Client Wrapper Class

So as you can imagine by the title, I was recently tasked with working on a command line project for MySQL. I wanted to be able to reuse whatever I made, so that meant creating a down and dirty wrapper class for some of the MySQL C API functions. This is my start. Another requirement was that my code pass the -ansi compiler check as well.

Here is my work-in-progress implementation with an executable proof of concept:

/*
* @author      randymelder@gmail.com
* @created     2010-05-24
* @version     alpha
*/
#include <mysql.h>
#include <iostream>
#include <iomanip>
using namespace std;
class MyWrapper {
private:
unsigned int    num_fields;
unsigned int    num_rows;
const char      *user;
const char      *pass;
const char      *host;
const char      *db;
char            *sql;
MYSQL           *conn;
MYSQL_RES       *res;
//MYSQL_ROW       row;
//
// Sets quantity of fields.
//
int setNumFields(unsigned int fq)
{
num_fields = fq;
return num_fields;
};
//
// Sets quantity of rows
//
int setNumRows(unsigned int rq)
{
num_rows = rq;
return num_rows;
};
public:
//
// MyWrapper Constructor
// @param const char *username,
// @param const char *password,
// @param const char *hostname,
// @param const char *database,
// @param bool open_connection = true
//
MyWrapper (const char *username,
const char *password,
const char *hostname,
const char *database,
bool open_connection = true)
{
user    = username;
pass    = password;
host    = hostname;
db      = database;
if(true == open_connection)
{
MyWrapper::connect();
}
};
//
// Connect to mysqld
// Returns true if connected.
// @return bool
//
bool connect() {
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, host, user, pass, db, 0, NULL, 0))
{
perror(mysql_error(conn));
return(false); //FALSE
}
else
return(true); //TRUE
};
//
// Send a SELECT statement to mysqld.
// Returns a MYSQL_RES result set of multi-dimensional char array.
// @param const char * SQL SELECT statement
// @return MYSQL_RES result set of multi-dimensional char array.
//
MYSQL_RES *sendQuery(const char *sql)
{
if(sql != NULL)
{
// send SQL query
if (mysql_query(conn, sql))
{
perror(mysql_error(conn));
exit(EXIT_FAILURE);
}
res = mysql_use_result(conn);
if(res == NULL)
{
return false;
}
else
{
MyWrapper::setNumFields(mysql_num_fields(res));
MyWrapper::setNumRows(mysql_num_rows(res));
return res;
}
}
else
return false; //false
}
//
// Returns the quantity of fields in the current result set.
//
int getNumFields()
{
return num_fields;
}
//
// Returns the quantity of rows in the current result set.
//
int getNumRows()
{
return num_rows;
}
//
// Destructor
//
~MyWrapper(){
mysql_free_result(res);
mysql_close(conn);
};
};
int main(int argc, char *argv[])
{
unsigned int num_fields = 0;
unsigned int iter       = 0;
MYSQL_RES *myres;
MYSQL_ROW myrow;
// 1. send credentials & test connection
MyWrapper *myw = new MyWrapper
("test",      // user
"test",      // pass
"localhost", // host
"test",      // database
true);       // connect immediately
// 2. send query & get result set
myres                = myw->sendQuery("show variables");
num_fields         = myw->getNumFields(); //mysql_num_fields(myres);
// 3. read result set
while ((myrow = mysql_fetch_row(myres)) != NULL)
{
for(iter = 0; iter < num_fields; ++iter)
{
if(!(iter % 2))
cout << setw(30) << setfill('-');
cout << myrow[iter] << left << " ";
}
cout << '\n';
}
// 4. close connection
delete myw;
return (EXIT_SUCCESS);
}

Discussion

2 comments for “ANSI C++ MySQL Client Wrapper Class”

  1. Thank you for this simple code. My server service doesn’t provide any MySQL C++ wrapper libraries, so I’ve been looking for something simple like this.

    Posted by petter | October 24, 2012, 2:26 am
  2. I really enjoy C++ and wish I had more time to write a more robust lib. This is a start, though. Have fun!

    Posted by Randy | March 19, 2013, 10:14 am

Post a comment

Help support my site and buy a domain name at http://domainsemailhosting.com/

%d bloggers like this: