sqlite_column_text converting result to a c++ string

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

sqlite_column_text converting result to a c++ string

Terry MacDonald-2
Hi,

New to the list, so apologies if this has already been answered, I just
can't find anything on my searches.

I am writings something in C++ and I am also using the
prepare/step/finalize approach to retrieve values from the sqlite
database: no callbacks - messy in C++.

There may be a simple answer to this but HOW do I get my return value
from using sqlite_column_text (a const unsigned char*) into a c++ string?

example...

   string zName = sqlite3_column_text(pStmt,0) ;

fails as does numerous variations using static_cast, const_cast

If it returned const char* no problem, but the const unsigned char*
aaaahhhh! the STL uses char* not unsigned char*

Please say I'm stoopid and the answer is straight forward.

Cheers
Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

njhinder
I am using C and do this:
char      *values;
values = (char *)sqlite3_column_text(pStmt, i);

and I don't have any problems..
I'm not sure if that helps you at all.





                                                                           
             Terry MacDonald                                              
             <terry.macdonald@                                            
             dsl.pipex.com>                                             To
                                       [hidden email]            
             10/17/2005 09:43                                           cc
             AM                                                            
                                                                   Subject
                                       [sqlite] sqlite_column_text        
             Please respond to         converting result to a c++ string  
             sqlite-users@sqli                                            
                  te.org                                                  
                                                                           
                                                                           
                                                                           
                                                                           





Hi,

New to the list, so apologies if this has already been answered, I just
can't find anything on my searches.

I am writings something in C++ and I am also using the
prepare/step/finalize approach to retrieve values from the sqlite
database: no callbacks - messy in C++.

There may be a simple answer to this but HOW do I get my return value
from using sqlite_column_text (a const unsigned char*) into a c++ string?

example...

   string zName = sqlite3_column_text(pStmt,0) ;

fails as does numerous variations using static_cast, const_cast

If it returned const char* no problem, but the const unsigned char*
aaaahhhh! the STL uses char* not unsigned char*

Please say I'm stoopid and the answer is straight forward.

Cheers


Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Jay Sprenkle
In reply to this post by Terry MacDonald-2
On 10/17/05, Terry MacDonald <[hidden email]> wrote:

> Hi,
>
> New to the list, so apologies if this has already been answered, I just
> can't find anything on my searches.
>
> I am writings something in C++ and I am also using the
> prepare/step/finalize approach to retrieve values from the sqlite
> database: no callbacks - messy in C++.
>
> There may be a simple answer to this but HOW do I get my return value
> from using sqlite_column_text (a const unsigned char*) into a c++ string?
>
> example...
>
>    string zName = sqlite3_column_text(pStmt,0) ;

I did it this way:
char* Sqlite::Column( const unsigned int Index )
  {
    char* p = (char*) sqlite3_column_text( pStmt, Index );
    return (char*) ( p ? p : "" );
  }

This checks for null column return values and returns an empty string.
I return everything as strings and handle conversion at a higher level.

I wrote a wrapper around sqlite for C++. I execute a query and return
the result set as an STL vector. It won't work for huge result sets because
it uses a lot of memory, but I seldom ever run a select against anything that
large anyway. For user interaction more than a page of results is generally
wasted.

Here are the wrapper class declarations:

//---------------------------------------------------------------------------
// a returned row from a query
// consists of a pair<string,string> returning column name and value
// operator() returns value given the column name
//---------------------------------------------------------------------------
class SqliteRow : public multimap<string,string>
  {
  public:
    const string  operator()( const string& key );
    void          operator+=( const pair<string,string>& column );

    void          dump();
  };

//---------------------------------------------------------------------------
// a returned set from a query
// consists of a vector of rows
//---------------------------------------------------------------------------
class SqliteSet : public vector<SqliteRow>
  {
  public:
    void          operator+=( const SqliteRow& row );

    void          dump();
  };

//---------------------------------------------------------------------------
// Sqlite wrapper
//---------------------------------------------------------------------------
class Sqlite
  {
    public:
      Sqlite( const string& filename );
      ~Sqlite();

      unsigned int              Changes();
      char*                     Column( const unsigned int Index );
      const char*               ColumnName( const unsigned int Index );

      SqliteRow                 ExecSingle( const string& sql );
      SqliteSet                 Select( const string& sql );
      unsigned int              Insert( const string& sql );
      unsigned int              Delete( const string& sql );
      unsigned int              Update( const string& sql );

      sqlite3*      db;
      sqlite3_stmt *pStmt;
    private:
      bool                      Prep( const string& sql );
      void                      Finalize();
      void                      Bind( const unsigned int Index, const
string* column );
      void                      Bind( const unsigned int Index, const
DateTime* column );
      int                       Step();

      SqliteRow                 Row();
  };
Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Eugene Wee
In reply to this post by Terry MacDonald-2
Hi,

You could use reinterpret_cast, though I'm still not sure if that is a good idea.

Eugene

Terry MacDonald wrote:

> Hi,
>
> New to the list, so apologies if this has already been answered, I just
> can't find anything on my searches.
>
> I am writings something in C++ and I am also using the
> prepare/step/finalize approach to retrieve values from the sqlite
> database: no callbacks - messy in C++.
>
> There may be a simple answer to this but HOW do I get my return value
> from using sqlite_column_text (a const unsigned char*) into a c++ string?
>
> example...
>
>   string zName = sqlite3_column_text(pStmt,0) ;
>
> fails as does numerous variations using static_cast, const_cast
>
> If it returned const char* no problem, but the const unsigned char*
> aaaahhhh! the STL uses char* not unsigned char*
>
> Please say I'm stoopid and the answer is straight forward.
>
> Cheers
>
>


Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Dennis Cote
In reply to this post by Terry MacDonald-2
Terry MacDonald wrote:

> Hi,
>
> New to the list, so apologies if this has already been answered, I
> just can't find anything on my searches.
>
> I am writings something in C++ and I am also using the
> prepare/step/finalize approach to retrieve values from the sqlite
> database: no callbacks - messy in C++.
>
> There may be a simple answer to this but HOW do I get my return value
> from using sqlite_column_text (a const unsigned char*) into a c++ string?
>
> example...
>
>   string zName = sqlite3_column_text(pStmt,0) ;
>
> fails as does numerous variations using static_cast, const_cast
>
> If it returned const char* no problem, but the const unsigned char*
> aaaahhhh! the STL uses char* not unsigned char*
>
> Please say I'm stoopid and the answer is straight forward.
>
> Cheers
>
Terry,

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.

HTH
Dennis Cote
Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Jay Sprenkle
> 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?
Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Dennis Cote
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);

Dennis Cote


Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Terry MacDonald-2
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 | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Dennis Cote
Terence MacDonald wrote:

>
>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?)
>
>
>  
>
Terry,

You might want to check out the C++ wrapper classes at
http://www.codeproject.com/database/CppSQLite.asp. There are also links
to other wrappers at http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers 
(scroll down to C++).

Dennis Cote
Reply | Threaded
Open this post in threaded view
|

Re: sqlite_column_text converting result to a c++ string

Jay Sprenkle
> >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?)
> >
> >
> >
> >
> Terry,
>
> You might want to check out the C++ wrapper classes at
> http://www.codeproject.com/database/CppSQLite.asp. There are also links
> to other wrappers at http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers
> (scroll down to C++).

Mine is very simple, but sufficient for my project.
These are definitely worth looking at.