Click here to Skip to main content
15,896,912 members
Articles / Database Development / MySQL

Accessing: MySQL Database using MySQL C API

Rate me:
Please Sign up or sign in to vote.
4.78/5 (22 votes)
1 Nov 2011CPOL6 min read 100.9K   7.2K   81  
A set of class to access and manipulate MySQL database using MySQL C API
//
// DataBase.h
//
// DataBase common interface.
//
// Copyright (c) 2009, Renato Tegon Forti - re.tf@acm.org
//
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


template<class T>
class DataBase
	/// DataBase common interface class
{
public:
	DataBase()
		: _connected(false)
	{
	}

	DataBase(const std::string& server, const std::string& user, const std::string& password, const std::string& database)
		: _connected(false)
	{
		connect(server, user, password, database);
	}
	
	virtual ~DataBase()
	{
		if(_connected)
			_dataBaseEngine.close();
	}
	
	void connect(const std::string& server, const std::string& user, const std::string& password, const std::string& database)
	{
		_dataBaseEngine.connect(server, user, password, database);

		_connected = true;
	}

	DataBase& operator<< (const std::string& sql)
	{
		_dataBaseEngine.execute(sql);

		return *this;
	}

	DataBase& operator, (ResultSet& rs)
	{
		_dataBaseEngine.populate(rs);

		return *this;
	}

private:
	T		_dataBaseEngine;
	bool	_connected;

}; // DataBase

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions