On Dec 5, 2008, at 2:19 AM, Brad Hubbard wrote:
> If I want to display the data in it->mobileNumber is the preferred
> method to do it like this?
>
> if( it->mobileNumber == mysqlpp::null )
> {
> something_that_requires_a_c_string = "";
> }
> else
> {
> something_that_requires_a_c_string = it-
> >mobileNumber.data.c_str(); // or it->mobileNumber.data
> }
If that's appropriate code for your application, it implies SQL NULL
has no special significance to you, so you should drop it from the DB.
If you can't change the DB, use the NullIsBlank behavior. If you do
that, the code can be:
something_that_requires_a_c_string = sql_char(it-
>mobileNumber).c_str();
You need the explicit conversion in there because C++ won't implicitly
do two casts in a row.
Actually, this might work, too:
something_that_requires_a_c_string = sql_char(it->mobileNumber);
...because sql_char has a const char* operator.
If SQL NULL does mean something different from blank in your
application, then I question whether the quoted code above is even
appropriate. Maybe different parts of your application have more
stringent requirements than others, but you should think about which
case applies, not just blindly cast away NULLness.
> The only example using char based data does this;
>
> if (it->description != mysqlpp::null) {
> cout << " (" << it->description << ")";
>
> but I presume it can do that because of an overloaded "<<" operator?
Not exactly. It works because Null<foo> can convert itself to foo, so
you can use Null<foo> anywhere you need a foo. This doesn't help you
because you need a double conversion.