On Mon, 2005-10-17 at 14:49 -0600, Dennis Cote wrote:
> Jay Sprenkle wrote:
> 
> >>To use the sqlite3 return value to initialize a standard string in C++
> >>you need to do the following:
> >>
> >>    const char* p = reinterpret_cast<const
> >>char*>(sqlite3_column_text(pStmt, 0));
> >>    std::string zName(p);
> >>
> >>The cast changes the type of the return value to match the type needed
> >>by the string constructor. Then construct a string using that pointer.
> >>You could combine the two statements into one and eliminate the
> >>temporary pointer variable if you prefer.
> >>    
> >>
> >
> >I believe that will crash if the column is null. What does your string
> >constructor do with null pointers?
> >
> >  
> >
> You are right, this will not work with NULL field values (it shouldn't 
> crash though, it should throw an exception). You will need to check for, 
> and handle, this special case in a manner appropriate for your 
> application. This might be sufficient:
> 
>   const char* p = reinterpret_cast<const char*>(sqlite3_column_text(pStmt, 
> 0));
>   if (p == NULL)
>     p = "<NULL>"; // or some other sentinel value  
>   std::string zName(p);

> 
Thanks for all of your replies, they have been very helpful.

The code finally compiled when I used a good ol' C cast and not the C++
static_cast or const_cast; which is good enough for now i.e.

    string name = (const char*)(sqlite3_column_text(pStmt,0)) ;

However I will look at using the reinterpret method and I will look more
at Jay's C++ wrapper (any chance of a peek at the src definitions?)

I now have another, different, issue which I will post as a new topic.

Thanks again


Reply via email to