On 10/17/05, Terry MacDonald <[EMAIL PROTECTED]> 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();
};