Download MySQL++ User Manual

Transcript
MySQL++ User Manual
cout.setf(ios::left);
for (unsigned int i = 0; i < res.names().size(); i++) {
cout << setw(2) << i
// this is the name of the field
<< setw(15) << res.names(i).c_str()
// this is the SQL identifier name
// Result::types(unsigned int) returns a mysql_type_info which in many
// ways is like type_info except that it has additional sql type
// information in it. (with one of the methods being sql_name())
<< setw(15) << res.types(i).sql_name()
// this is the C++ identifier name which most closely resembles
// the sql name (its is implementation defined and often not very readable)
<< setw(20) << res.types(i).name()
<< endl;
}
cout << endl;
if (res.types(0) == typeid(string)) {
// this is demonstrating how a mysql_type_info can be
// compared with a C++ type_info.
cout << "Field 'item' is of an SQL type which most "
"closely resembles\nthe C++ string type\n";
}
if (res.types(1) == typeid(longlong)) {
cout << "Field 'num' is of an SQL type which most "
"closely resembles\nC++ long long int type\n";
}
else if (res.types(1).base_type() == typeid(longlong)) {
// you have to be careful as if it can be null the actual
// type is Null<TYPE> not TYPE. So you should always use
// the base_type method to get at the underlying type.
// If the type is not null than this base type would be
// the same as its type.
cout << "Field 'num' base type is of an SQL type which "
"most closely\nresembles the C++ long long int type\n";
}
}
catch (const BadQuery& er) {
// Handle any query errors
cerr << "Query error: " << er.what() << endl;
return -1;
}
catch (const BadConversion& er) {
// Handle bad conversions
cerr << "Conversion error: " << er.what() << endl <<
"\tretrieved data size: " << er.retrieved <<
", actual size: " << er.actual_size << endl;
return -1;
}
catch (const Exception& er) {
// Catch-all for any other MySQL++ exceptions
cerr << "Error: " << er.what() << endl;
return -1;
}
return 0;
}
3.11. Let's Do Something Useful
These next few examples demonstrate just how powerful C++ can be, allowing you to do a lot of work in few lines
of code without losing efficiency.
Since the code is meant to be re-used as-is, constants that can differ from one case to another have been grouped in
20