In practice of course, not all data is going to consist of strings.
Many fields will be integer values, or decimals, or Booleans. To
convert the field's value to one of these, use its
to
method. This adapts itself to the type of
variable you pass it, expecting the field value to be of an
appropriate form for that type. For convenience,
to
returns false
if the field
had the null value, and true
otherwise. In the
former case, the variable will retain the value it had before the
call.
// Pay an employee their salary. Add bonus for managers. // The employee row must contain the fields void PaySalary(result::const_iterator empl) { long id; float salary; bool is_manager=false; // Get id. Must never be null. if (!empl[0].to(id)) throw runtime_error("No id!"); // Get salary. If this employee has no salary, skip payment. if (!empl[1].to(salary)) return; // Get manager status. If field is null, this will leave the // variable's original value unchanged. empl[2].to(is_manager); if (is_manager) salary += Bonus; TransferMoney(id, salary); }
If conversion fails, e.g. when trying to convert a floating-point
value to an integer variable, to
will throw a
runtime_error
.