lang="en-US"> C++ MySQL Database Class –  Design1online.com, LLC

C++ MySQL Database Class

/**************
* File Name: database.cpp
* Author: Design1online.com, LLC
* Purpose: mysql database class
**************/
#include "database.h"

/**************
* Purpose: default constructor, initialize class values
* Precondition: none
* Postcondition: none
**************/
database::database()
{
 //initilize the mysql fields
 sock = NULL;
 row = NULL;
 result = NULL;
 field = NULL;          
}

/**************
* Purpose: connect to the appropriate database
* Precondition: host, database, user name and password
* Postcondition: connected to that database
**************/
int database::openConnection(char *host, char *db, char *user, char *pass)
{
 if (sock) //already connected to another database
 disconnect(); //disconnect from that one

 //initilize the socket
 sock = mysql_init(0);

 //something went wrong with the socket
 if (!sock)
 return ERR201;

 //try connecting to the database  
 if (!mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
 return ERR202;

 //successfully connected to the database    
 return SUCCESS;  
}

/**************
* Purpose: disconnect from the database
* Precondition: none
* Postcondition: socket closed
**************/
bool database::disconnect()
{
 if (sock) //they have a socket open
 mysql_close(sock);

 //release result data
 free(); 

 //database disconnected
 return true;
}

/**************
* Purpose: free the results from the database query
* Precondition: none
* Postcondition: results no longer there
**************/
bool database::free()
{
 if (result)
 {
 mysql_free_result(result);
 return true;
 }

 return false;
}

/**************
* Purpose: return the approprite error message
* Precondition: error code
* Postcondition: string with the error returned
**************/
char *dberror(int errorcode)
{

 //display the appropriate error message for this error    
 switch(errorcode)
 {
 case SUCCESS: return "SUCCESS";
 break;

 case ERR201: return "201 SOCKET ERROR: SOCKET FAILURE";
 break;

 case ERR202: return "202 CONNECTION ERROR: CANNOT ACCESS THE SERVER";
 break;

 case ERR203: return "203 DATABASE ERROR: QUERY FAILED";
 break;
 }

 return NULL; //no error, return null
}

/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: result set returned (or null)
**************/
MYSQL_RES *database::query(char *query)
{        
 //query the database
 mysql_query(sock, query);

 //store the results
 result = mysql_store_result(sock);

 return result;

}

/**************
* Purpose: update the database no result returned
* Precondition: the query string
* Postcondition: false if failed, true if suceess
**************/
bool database::updateQuery(char *query)
{        
 if (!mysql_query(sock, query))
 return 0; //failed query
 else
 return 1; //successful query
}

/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: the FIRST result is returned (or null)
*                will not return multiple rows, only the first
**************/
char *database::stringQuery(char *query)
{
 //if old results exist, free them
 //free();

 //query the database
 mysql_query(sock,query);

 //store the results
 result = mysql_store_result(sock);

 if (!result)
 return NULL; //no results

 //fetch the row
 row = mysql_fetch_row(result);

 //store the result & convert it to a number
 char *stringResult = row[0];

 //free the results
 free();

 return stringResult;

}

/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: the FIRST result is returned (or null)
*                will not return multiple rows, only the first
**************/
int database::intQuery(char *query)
{        
 //query the database
 mysql_query(sock,query);

 //store the results
 result = mysql_store_result(sock);

 if (!result)
 return -1; //no results

 //fetch the row
 row = mysql_fetch_row(result);

 //store the result & convert it to a number
 int id = atoi(row[0]);

 //free the results
 free();

 return id; //return the id number

}

/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: the FIRST result is returned (or null)
*                will not return multiple rows, only the first
**************/
bool database::boolQuery(char *query)
{

 //query the database
 mysql_query(sock, query);

 //store the results
 result = mysql_store_result(sock);

 return (bool)row[0];

}

/**************
* File Name: database.h
* Author: jade@design1online.com
* Purpose: header file for database.cpp class
**************/
#ifndef DATABASE_H
#define DATABASE_H

#include "mysql/mysql.h"

/**************
* Database Error Definitions
**************/
#define SUCCESS 200
#define ERR201 201
#define ERR202 202
#define ERR203 203
#define MAX_QUERY_LEN 500

class database
{
 public:
 //class constructor
 database(); 

 //connection & disconnect
 int       openConnection(char *host, char *db, char *user, char *pass); //connect to the database
 bool      disconnect(); //disconnect from the database

 //querying
 MYSQL_RES *query(char *query); //return result set
 char      *stringQuery(char *query); //return string
 int       intQuery(char *query); //return an integer
 bool      boolQuery(char *query); //return a boolean
 bool      updateQuery(char *query); //updates, returns true if update went through

 //free the query results
 bool free();

 private:
 MYSQL *sock;
 MYSQL_RES *result;
 MYSQL_ROW row;
 MYSQL_FIELD *field;
 char *qbuf[MAX_QUERY_LEN];
};

char *dberror(int errorcode); //return a string for this error message

#endif

In order for this to work correctly you also need the libmysql.dll file in any of your compiled projects and you’ll also need to setup mysql.h header files to link them in during compilation.

Programming Challenge Main Page or Continue to Day 4

You may also like...

2 Responses

Leave a Reply