1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include <iostream>
#include <mysql.h>
using namespace std;
struct MySQLRS{
public:
MySQLRS(MYSQL *pMySQL);
~MySQLRS();
MYSQL_RES *ptrResult;
MYSQL_ROW row;
MYSQL_FIELD *ptrField;
void setTotalRows(int pnTotalRows);
int getTotalRows();
int isEOF();
void setEOF(bool pnEOF);
void moveNext();
void freeResult();
private:
MYSQL *ptrMySQL;
long _nTotalRows;
int _nCurrentRow;
bool _bEOF;
};
void MySQLRS::freeResult(){
mysql_free_result( ptrResult );
return;
}
void MySQLRS::setEOF(bool pnEOF){
_bEOF = pnEOF;
return;
};
int MySQLRS::isEOF(){
return _bEOF;
};
void MySQLRS::setTotalRows(int pnTotalRows){
_nTotalRows = pnTotalRows;
};
int MySQLRS::getTotalRows(){
return _nTotalRows;
};
void MySQLRS::moveNext(){
if( (_nCurrentRow + 1) >= _nTotalRows ){
MySQLRS::freeResult();
_bEOF = true;
}else{
mysql_data_seek(ptrResult, _nCurrentRow + 1);
row = mysql_fetch_row(ptrResult);
if( row == NULL ){
MySQLRS::freeResult();
_bEOF = true;
}else{
_nCurrentRow++;
}
}
};
MySQLRS::MySQLRS(MYSQL *pMySQL){
ptrResult=0; ptrField=0; _nTotalRows=0; _nCurrentRow=0; _bEOF=false; row=NULL;
ptrMySQL = pMySQL;
};
MySQLRS::~MySQLRS(){
delete ptrResult; ptrResult=0; delete ptrField; ptrField=0;
};
typedef struct MySQLRS *ptrMySQLRS;
class MySQL{
public:
MySQL();
~MySQL();
string sHostName;
string sUserName;
string sPassword;
string sErrorMsg;
int nErrorNo;
string sDatabase;
int Connect(string psHostName, string psUserName, string psPassword, string psDatabase);
void Close();
int Query(char *psQuery);
ptrMySQLRS Execute(char *psQuery);
MYSQL *ptrMYSQL;
};
ptrMySQLRS MySQL::Execute(char *psQuery){
ptrMySQLRS RS;
RS = new MySQLRS(ptrMYSQL);
if( mysql_query(ptrMYSQL, psQuery) != 0 ){
sErrorMsg = mysql_error(ptrMYSQL);
nErrorNo = mysql_errno(ptrMYSQL);
RS->setEOF(true);
}else{
RS->ptrResult = mysql_store_result(ptrMYSQL);
RS->setTotalRows( (long)mysql_num_rows (RS->ptrResult) );
RS->row = mysql_fetch_row(RS->ptrResult);
if(RS->getTotalRows() > 0){
RS->setEOF(false);
}
}
return RS;
};
MySQL::MySQL(){
sHostName="";sUserName="";sPassword="";sErrorMsg="";sDatabase="";
ptrMYSQL = 0;
};
MySQL::~MySQL(){
MySQL::Close();
delete ptrMYSQL; ptrMYSQL=0;
};
int MySQL::Connect(string psHostName, string psUserName, string psPassword, string psDatabase){
sHostName = psHostName;
sUserName = psUserName;
sPassword = psPassword;
sDatabase = psDatabase;
ptrMYSQL = mysql_init(NULL);
if(ptrMYSQL == NULL){
sErrorMsg = "mysql_init() Error";
return 0;
}
if(!mysql_real_connect(ptrMYSQL, MySQL::sHostName.c_str(), MySQL::sUserName.c_str(), MySQL::sPassword.c_str(), MySQL::sDatabase.c_str(), 0, NULL, 0)){
sErrorMsg = mysql_error(ptrMYSQL);
nErrorNo = mysql_errno(ptrMYSQL);
return 0;
}
return 1;
};
void MySQL::Close(){
mysql_close(ptrMYSQL);
};
int MySQL::Query(char *psQuery){
if(mysql_query(ptrMYSQL, psQuery) != 0){
sErrorMsg = mysql_error(ptrMYSQL);
nErrorNo = mysql_errno(ptrMYSQL);
return 0;
}
return 1;
}
int main(int argc, char *args[]){
MySQL *ptrMySQL = new MySQL;
MySQL *ptrMySQL2 = new MySQL;
MySQLRS *RS = 0;
MySQLRS *RS2 = 0;
char sQuery[200];
ptrMySQL->Connect("localhost", "root", "", "tones_config");
ptrMySQL2->Connect("localhost", "root", "", "tones_config");
RS = ptrMySQL->Execute("SELECT * FROM keyword_list LIMIT 5");
while( !RS->isEOF() ){
cout << "RS1 => " << RS->row[0] << endl;
RS2 = ptrMySQL2->Execute("SELECT * FROM keyword_list LIMIT 5");
while(!RS2->isEOF()){
cout << "RS2 => " << RS2->row[0] << endl;
RS2->moveNext();
}
cout << endl << endl;
RS->moveNext();
}
delete RS; RS=0;
delete RS2; RS2=0;
delete ptrMySQL; ptrMySQL=0;
delete ptrMySQL2; ptrMySQL2=0;
return 0;
}
|