124{
127
128
130
131 try{
132
134 {
137 }
138
139 int status = mysql_real_query(m_conn, sql.c_str(), sql.length());
140 if (status)
141 {
142
143
144 std::cerr << "MySQL error: MySQL server has gone away"<< std::endl;
146
147 sleep(5);
149 if (ret == 0) {
150 std::cout << "Connected to MySQL database " << std::endl;
152 status = mysql_real_query(m_conn, sql.c_str(), sql.length());
153 }
154
155
156 if(status)
157 {
158 cerr << "Query " << sql << " failed: " << mysql_error(m_conn) << endl;
159 return -1;
160 }
161 }
162
163 MYSQL_RES* result = mysql_store_result(m_conn);
164
165 if(result)
166 {
167 int num_fields = mysql_num_fields(result);
168
169 if(num_fields>0)
170 {
171 MYSQL_FIELD *fields;
172 fields = mysql_fetch_fields(result);
173
174 MYSQL_ROW row;
175 while ((row = mysql_fetch_row(result)))
176 {
177 unsigned long* lengths;
178 lengths = mysql_fetch_lengths(result);
180 int field;
181 for(field = 0; field<num_fields; field++)
182 {
183 if(row[field]!=0)
184 {
185 unsigned long field_len = lengths[field];
186 char* new_record;
187 if(fields[field].type==FIELD_TYPE_BLOB)
188 {
189 new_record = new char[field_len];
190 memcpy(new_record, row[field], field_len);
191 }
192 else
193 {
194 new_record = new char[field_len+1];
195 strcpy(new_record, row[field]);
196 }
197
198 (*dbrec)[fields[field].name]=new_record;
199 }
200 else
201 {
202 (*dbrec)[fields[field].name]='\0';
203 }
204 }
205 records.push_back(dbrec);
206 }
207 }
208 mysql_free_result(result);
211
212 return records.size();
213 }
214 }
215 catch(...)
216 {
217 cerr << "Could not execute query: " << mysql_error(m_conn) << endl;
220 return -1;
221 }
222
225
226 return 0;
227}
struct st_mysql_res MYSQL_RES