28 for(iattempt=0;iattempt<3;iattempt++)
30 ret = mysql_real_connect(m_conn, host.c_str(), user.c_str(), passwd.c_str(),
m_dbName.c_str(),port,
NULL,0);
32 std::cout <<
"DatabaseSvc: Connected to MySQL database" << std::endl;
36 std::cout <<
"Couldn't connect to MySQL database. Trying again." << std::endl;
41 if(ret == 0 && iattempt == 2)
42 throw (
char*)mysql_error(m_conn);
44 }
catch ( std::exception &e ) {
46 cerr <<
"Error in MySQL session initialization!" << endl;
47 cerr <<
"*** std::exception caught:" << endl;
48 cerr <<
"*** error message:" << e.what() << endl;
51 }
catch (...) {
return -1; }
59 int ret = mysql_select_db(m_conn, dbname.c_str());
66 ret = mysql_select_db(m_conn, dbname.c_str());
69 throw std::exception();
88 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
93 std::cerr <<
"MySQL error: MySQL server has gone away"<< std::endl;
99 std::cout <<
"Connected to MySQL database " << std::endl;
101 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
107 cerr <<
"Query " << sql <<
" failed: " << mysql_error(m_conn) << endl;
114 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)