27 for(iattempt=0;iattempt<3;iattempt++)
29 ret = mysql_real_connect(m_conn, host.c_str(), user.c_str(), passwd.c_str(),
m_dbName.c_str(),3306,NULL,0);
31 std::cout <<
"DatabaseSvc: Connected to MySQL database" << std::endl;
35 std::cout <<
"Couldn't connect to MySQL database. Trying again." << std::endl;
40 if(ret == 0 && iattempt == 2)
41 throw (
char*)mysql_error(m_conn);
43 }
catch ( std::exception &e ) {
45 cerr <<
"Error in MySQL session initialization!" << endl;
46 cerr <<
"*** std::exception caught:" << endl;
47 cerr <<
"*** error message:" << e.what() << endl;
50 }
catch (...) {
return -1; }
58 int ret = mysql_select_db(m_conn, dbname.c_str());
65 ret = mysql_select_db(m_conn, dbname.c_str());
68 throw std::exception();
87 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
92 std::cerr <<
"MySQL error: MySQL server has gone away"<< std::endl;
98 std::cout <<
"Connected to MySQL database " << std::endl;
100 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
106 cerr <<
"Query " << sql <<
" failed: " << mysql_error(m_conn) << endl;
113 cerr <<
"Could not execute query: " << mysql_error(m_conn) << endl;
138 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
143 std::cerr <<
"MySQL error: MySQL server has gone away"<< std::endl;
149 std::cout <<
"Connected to MySQL database " << std::endl;
151 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
157 cerr <<
"Query " << sql <<
" failed: " << mysql_error(m_conn) << endl;
162 MYSQL_RES* result = mysql_store_result(m_conn);
166 int num_fields = mysql_num_fields(result);
171 fields = mysql_fetch_fields(result);
174 while ((row = mysql_fetch_row(result)))
176 unsigned long* lengths;
177 lengths = mysql_fetch_lengths(result);
180 for(field = 0; field<num_fields; field++)
184 unsigned long field_len = lengths[field];
186 if(fields[field].type==FIELD_TYPE_BLOB)
188 new_record =
new char[field_len];
189 memcpy(new_record, row[field], field_len);
193 new_record =
new char[field_len+1];
194 strcpy(new_record, row[field]);
197 (*dbrec)[fields[field].name]=new_record;
201 (*dbrec)[fields[field].name]=
'\0';
204 records.push_back(dbrec);
207 mysql_free_result(result);
211 return records.size();
216 cerr <<
"Could not execute query: " << mysql_error(m_conn) << endl;
struct st_mysql_res MYSQL_RES
int query(std::string dbname, std::string query)
int select_db(std::string dbname)