class MySQLManager { public: /* * Init MySQL * @param hosts: Host IP address * @param userName: Login UserName * @param password: Login Password * @param dbName: Database Name * @param port: Host listen port number */ MySQLManager(std::string hosts, std::string userName, std::string password, std::string dbName, unsigned int port); ~MySQLManager(); void initConnection(); /* * Making query from database * @param mysql: MySQL Object * @param sql: Running SQL command */ bool runSQLCommand(std::string sql); /** * Destroy MySQL object * @param mysql MySQL object */ void destroyConnection(); bool getConnectionStatus(); vector< vector<string> > getResult(); protected: void setUserName(std::string userName); void setHosts(std::string hosts); void setPassword(std::string password); void setDBName(std::string dbName); void setPort(unsigned int port); private: bool IsConnected; vector< vector<string> > resultList; MYSQL mySQLClient; unsigned int DEFAULTPORT; char * HOSTS; char * USERNAME; char * PASSWORD; char * DBNAME; };
#endif /* MYSQLMANAGER_H_ */
/* * MySQLManager.cpp * * Created on: Feb 18, 2009 * Author: Steven Wee */ #include "MySQLManager.h"
MySQLManager::MySQLManager(string hosts, string userName, string password, string dbName, unsigned int port) { IsConnected = false; this ->setHosts(hosts); // 设置主机IP地址 this ->setUserName(userName); // 设置登录用户名 this ->setPassword(password); // 设置登录密码 this ->setDBName(dbName); // 设置数据库名 this ->setPort(port); // 设置端口号 }
MySQLManager::~MySQLManager() { this ->destroyConnection(); }
void MySQLManager::setDBName(string dbName) { if ( dbName.empty() ) {// 用户没有指定数据库名 std::cout << "DBName is null! Used default value: mysql" << std::endl; this ->DBNAME = new char[5]; strcpy(this ->DBNAME, "mysql"); } else { this ->DBNAME = new char[dbName.length()]; strcpy(this ->DBNAME, dbName.c_str()); } }
void MySQLManager::setHosts(string hosts) { if ( hosts.empty() ) {// 用户没有指定数据库IP地址 std::cout << "Hosts is null! Used default value: localhost" << std::endl; this ->HOSTS = new char[9]; strcpy(this ->HOSTS, "localhost"); } else { this ->HOSTS = new char[hosts.length()]; strcpy(this ->HOSTS, hosts.c_str()); } }
void MySQLManager::setPassword(string password) {// 用户没有指定密码 if ( password.empty() ) { std::cout << "Password is null! Used default value: " << std::endl; this ->PASSWORD = new char[1]; strcpy(this ->PASSWORD, ""); } else { this ->PASSWORD = new char[password.length()]; strcpy(this ->PASSWORD, password.c_str()); } }
void MySQLManager::setPort(unsigned int port) {// 用户没有指定端口号,使用默认端口号 if ( port ) { std::cout << "Port number is null! Used default value: 0" << std::endl; this ->DEFAULTPORT = 0; } else { this ->DEFAULTPORT = port; } }
void MySQLManager::setUserName(string userName) {// 用户没有指定登录用户名 if ( userName.empty() ) { std::cout << "UserName is null! Used default value: root" << std::endl; this ->USERNAME = new char[4]; strcpy(this ->USERNAME, "root"); } else { this ->USERNAME = new char[userName.length()]; strcpy(this ->USERNAME, userName.c_str()); } }
i = mysql_real_query(&mySQLClient,sql.c_str(),(unsigned int)strlen(sql.c_str()));// 执行查询 if ( i ) { std::cout << "Error query from database: %s\n" << mysql_error(&mySQLClient) << std::endl; return false; } res = mysql_store_result(&mySQLClient); vector<string> objectValue; while( (row = mysql_fetch_row(res)) ) {// 遍历结果集 objectValue.clear(); for ( j = 0 ; j < mysql_num_fields(res) ; j++ ) { objectValue.push_back(row[j]); } this ->resultList.push_back(objectValue); } mysql_free_result(res); //free result after you get the result