/* * ===================================================================================== * * Filename: testMysql.cpp * * Description: * * Version: 1.0 * Created: 01/17/13 11:38:31 * Revision: none * Compiler: gcc * * Author: sciroccoseeker (sccs), tongpeixi@163.com * Company: * * ===================================================================================== */ #include #include #include #include #include #include #include using namespace std; class CMysqlException:public std::exception { public: CMysqlException(const string &sErrorMsg, const string &sSql, int error ) { m_str = sErrorMsg; m_sSql = sSql; m_errorno = error; } ~CMysqlException()throw() { } const char* GetErrMsg() { return m_str.c_str(); } const string &GetSqlStr() { return m_sSql; } int GetErrno() { return m_errorno; } const char *what() const throw() { return m_str.c_str(); } public: int m_errorno; string m_str; string m_sSql; }; int main(int argc, char* argv[]) { stringstream ssSql; try { string m_host="127.0.0.1"; int m_port = 3306; string m_user="root"; string m_pass=""; string m_charset="latin1"; char m_ErrMsg[1024]; unsigned int m_iRows; unsigned int m_iField; MYSQL_ROW m_row; ssSql << "select * from mysql.user;"; MYSQL m_connection; MYSQL_RES *m_result; mysql_init(&m_connection); mysql_options(&m_connection,MYSQL_SET_CHARSET_NAME, m_charset.c_str()); my_bool bReconnect =1; mysql_options(&m_connection, MYSQL_OPT_RECONNECT, &bReconnect); //conecting if (mysql_real_connect(&m_connection, m_host.c_str(), m_user.c_str(), m_pass.c_str(), NULL, m_port, NULL, CLIENT_MULTI_STATEMENTS) == NULL) { snprintf(m_ErrMsg, sizeof(m_ErrMsg), "connect[-h%s -u%s -p%s] fail.\nError %u (%s) %s:%d\n", m_host.c_str(), m_user.c_str(), m_pass.c_str(), mysql_errno(&m_connection), mysql_error(&m_connection), __FILE__ , __LINE__); throw CMysqlException(m_ErrMsg, "", mysql_errno(&m_connection)); } string sqlString = ssSql.str(); if (mysql_real_query(&m_connection, sqlString.c_str(), sqlString.length()) != 0) { stringstream ss; ss << __FILE__ << ":" << __LINE__ << " " << mysql_error(&m_connection) << ";host=" << m_host; throw CMysqlException(ss.str(), sqlString, mysql_errno(&m_connection)); } //store result m_result = mysql_store_result (&m_connection); if (m_result == NULL) { snprintf(m_ErrMsg, sizeof(m_ErrMsg), "save result%s!", mysql_error(&m_connection)); throw CMysqlException(m_ErrMsg, "", mysql_errno(&m_connection)); } m_iField = mysql_num_fields (m_result); m_iRows = mysql_num_rows (m_result); map m_FieldIndex; if (m_result!=NULL) { unsigned int i; MYSQL_FIELD *fields; fields = mysql_fetch_fields(m_result); for(i = 0; i < m_iField; i++) { m_FieldIndex[fields[i].name] = i; //m_FieldIndex[mysql_fetch_fields(m_result)[i].name] = i; } } //m_row = mysql_fetch_row (m_result); string sHost, sUser, sPasswd; while((m_row=mysql_fetch_row(m_result))) { sHost.assign(m_row[m_FieldIndex["Host"]]); sUser.assign(m_row[m_FieldIndex["User"]]); sPasswd.assign(m_row[m_FieldIndex["Password"]]); } } catch(CMysqlException& e) { cout << "error " << e.GetErrMsg() << endl; return -1; } catch(exception& e) { cout << "error "<< e.what() << endl; return -1; } cout << "success"<