Click here to Skip to main content
13,045,285 members (50,049 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 26 Feb 2007

Lean and extensible mySQL C++ wrapper

, 26 Feb 2007
Rate this:
Please Sign up or sign in to vote.
An article on a lean (2 header files) C++ template class that provides mySQL query


This article present a lean C++ wrapper of mySQL client. All of the functions are defined in two header files. Users can build the file with mySQL C API library and header files of boost::shared_ptr which is used to manage the MYSQL connection and result set.

Using the code

The classes diagram of the mySQL client wrapper is shown as the following diagram.

Screenshot - mysql_simple_wrapper_1.jpg

The structure is pretty simple. The only special note is the member function CMysqlConn::Query. The function has a template parameter 'ProcessQuery' which is a 'policy' class to cope with the concreted client requirements of using the API. Different clients have different purposes of querying a database. Some clients want to fetch the data that meets certain predications. Some clients want to insert and update records in which no return data is required. Other clients may just fetch the ID type of data that meets given 'where' clause. Inspired by the policy based solution addressed in Andrei Alexandrescu's "Modern C++ design", the 'Query' member function uses the template solution to separate the usage policy from the general query algorithm.

I have defined three policy classes for the CMysqlConn::Query. Here are the use cases.

  1. Query DB that needs returned data

    CMysqlSet rset=conn.Query<WithData>("select * from mytable");

  2. Query DB that does not need returned data

    bool ret =conn.Query<NoData>("insert into mytable values 
                        (5000, 'testing message')");

  3. Query DB that only fetches one value

    pair<bool, string> value=conn.Query<CheckOneRecord>
            ("select field from mytable where errorcode='2005'"); 

Users can define their own policy class by following this sample:

struct Nodata
    //Define your return type
    typedef boolReturnType;

    //Define your failed return function for the query failure case
    static ReturnType ReturnInFail() {return false;}

    //Define your records processing implementation after the 
    //query statement is successfully issued.
    static ReturnType DeepQuery(ConnPtr ptr) {return true;}

Here is the sample code of using this wrapper:

int Test()
 string query="select * from mytable";
 CMysqlConn conn("myhost","mytable", "login","password");
 if (conn)
    CMysqlSet rset=conn.Query<WithData>(query);
    if (rset)
      cout<<"I got a result";
      unsigned size=rset.NumberOfRecords();

     for (unsigned int i=0;i<size; i++){
       CMysqlRow row=rset.GetNextRow();
       if (row)
        for (unsigned int j=0; j<row.NumberOfFields();j++)
  cout<<"Last error is 

   string updateQuery="insert into mytable values (5000, 'testing message')";

  // Query single data test
  cout<<"query single data..."<<endl;
  pair<bool, string> value=conn.Query<CheckOneRecord>
            ("select filed1s from mytable where filed1='2005'");
  if (value.first)
   cout<<"get value of "<<value.second<<endl;
  return 0;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Richard Lin
United States United States
Richard Lin is senior software engineer of in Silicon Valley.

Richard Lin was born in Beijing and came to US in the fall of 1995. He began his first software career in bay area of California in 1997. He has worked for many interesting projects including manufacturing testing systems, wireless AP firmware and applications, email anti-virus system and personal firewalls. He loves playing go (WeiQi in Chinese) and soccer in his spare time. He has a beautiful wife and a cute daughter and enjoys his life in San Jose of California.

You may also be interested in...

Comments and Discussions

Generalhi can you please provide more info Pin
cordoval19-Mar-07 9:14
membercordoval19-Mar-07 9:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Feb 2007
Article Copyright 2007 by Richard Lin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid