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