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(),port,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;
139 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
144 std::cerr <<
"MySQL error: MySQL server has gone away"<< std::endl;
150 std::cout <<
"Connected to MySQL database " << std::endl;
152 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
158 cerr <<
"Query " << sql <<
" failed: " << mysql_error(m_conn) << endl;
163 MYSQL_RES* result = mysql_store_result(m_conn);
167 int num_fields = mysql_num_fields(result);
172 fields = mysql_fetch_fields(result);
175 while ((row = mysql_fetch_row(result)))
177 unsigned long* lengths;
178 lengths = mysql_fetch_lengths(result);
181 for(field = 0; field<num_fields; field++)
185 unsigned long field_len = lengths[field];
187 if(fields[field].type==FIELD_TYPE_BLOB)
189 new_record =
new char[field_len];
190 memcpy(new_record, row[field], field_len);
194 new_record =
new char[field_len+1];
195 strcpy(new_record, row[field]);
198 (*dbrec)[fields[field].name]=new_record;
202 (*dbrec)[fields[field].name]=
'\0';
205 records.push_back(dbrec);
208 mysql_free_result(result);
212 return records.size();
217 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)