Thanks Igor I used your solution and it work. Char* was the problem. I'll use 
strings from now on . 
   
  Stephen 

Igor Mironchick <[EMAIL PROTECTED]> wrote:
  I think problem is in use char * as parameter in push function. For that 
reason I use std::string always.
Look, in callback2 SQLite pass char ** - array of char*. But when your 
code leave callback2 SQLite might errase that array and then all char* 
will garbage. But when in push method you will pass const std::string & 
-- char* will transform into std::string and you will not lose any data. 
Or event you can obviosly call ctor of std::string like

q->push( atoi(argv[0] ),atoi(argv[1] ),atoi(argv[2] ), std::string( 
argv[3] ), std::string( argv[4] ) );

and push method then will looks like this:

void QuestionDB::push( int b, int c, int v, const std::string & q, const 
std::string & a )
{
...
}

P.S. Try don't use char* in C++ code. Use std::string instead. And when 
you need char* you can transform std::string into char* with c_str() 
method of std::string.


Stephen Sutherland wrote:
> Igor I tried your solution and can't get it working . 
> Here is my code. 
> 
> The STRANGE problem that I am having is that when it adds to the vector at 
> position 1, the vector contains the object. 
> 
> But when it adds to vector at position 2 using push_back - for some reason 
> the contents at vector 1 is garbage. 
> 
> then when it adds to vector at position 3 using push_back - for some reason 
> position 3 is garabage ? 
> 
> the errors in this situation doesn't seem to follow much logic ? but it's 
> consistent.
> 
> for example if i run this code and it uses push 4 times, position 1 and 3 
> might have garbase while position 2 will have the same contents as position 4 
> ? ?
> 
> this is some weird stuff i haven't seen before. 
> I'm wondering if the problem is due to the scope of either my 
> QuestionDBStruct or my vector . 
> 
> =================================================
> 
> static int callback2(void *NotUsed, int argc, char **argv, char **azColName)
> {
> int i;
> for(i=0; i> {
> printf("%i. %s = %s\n", i, azColName[i], argv[i] ? argv[i] : "NULL"); 
> } 
> QuestionDB* q = (QuestionDB*)NotUsed; 
> q->push(c );
> 
> return 0;
> }; 
> QuestionDB::QuestionDB()
> {
> 
> };
> void QuestionDB::push(int b, int c, int v, char* q, char* a)
> { 
> 
> 
> QuestionDBStruct qbs; 
> qbs.bible_book =1;
> qbs.bible_chapter =2; 
> qbs.bible_verse =3; 
> qbs.bible_answer ="test";
> qbs.bible_question =q;
> 
> printf("\n****** push called ************\n"); 
> 
> vecQuestions.push_back(qbs); 
> for(int x = 0 ; x < vecQuestions.size(); x++)
> printf("\nvecQuestion[%i] = %s \n", x, 
> ((QuestionDBStruct)vecQuestions[x]).bible_question );
> 
> 
> printf("\n*******************************\n");
> 
> }; ========================================================
> 
> 
> 
> 
> 
> 
> 
>
> Igor Mironchick wrote:
> If I understand you right then try it:
>
> static int add_value( void *st, int, char **value, char ** )
> {
> storage_t * storage = (storage_t*) st;
> st->push( value[ 0 ] );
> return SQLITE_OK;
> };
>
> class storage_t {
> public:
> storage_t()
> : m_db( 0 )
> {
> sqlite3_open( "your_database.db", &m_db );
> };
> virtual ~storage_t()
> {
> sqlite3_close( m_db );
> };
>
> void push( const std::string & v )
> {
> m_buff.push_back( v );
> }
>
> void read_table()
> {
> sqlite3_exec( m_db, "SELECT * FROM some_table",
> add_value, this, NULL );
> }
>
> private:
> sqlite3 * m_db;
> std::vector< std::string > m_buff;
> };
>
> This is very simple example, but it can help you I think.
>
> Stephen Sutherland wrote:
> 
>> Hi 
>>
>> I am using the 'quick start' C-styled code for sqlite3 
>> http://www.sqlite.org/quickstart.html
>> I think I'm running into a problem trying to put it in classes to make it 
>> somewhat object oriented. So I'm asking for help about how to make it 
>> object-oriented - or to confirm whether what I'm doing is object oriented. 
>> Here is the code: 
>>
>> [code]
>> //callback function
>> static int callback(void *NotUsed, int argc, char **argv, char **azColName)
>> {
>> int i;
>> for(i=0; i> printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
>> }
>> printf("\n");
>> return 0;
>> };
>>
>> // this callback is referenced here. 
>> void MyClass::executeSQLStatement()
>> {
>> rc = sqlite3_exec(db, "select * from table1" , callback, 0, &zErrMsg);
>> };
>>
>> [/code]
>>
>>
>> However I am trying to add a vector in the callback function to store the 
>> results. When I put the vector in it seems I am forced to do something like 
>> this:
>>
>>
>> [code]
>> vector vecX;
>>
>> static int callback(void *NotUsed, int argc, char **argv, char **azColName)
>> {
>> int i;
>> for(i=0; i> printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
>> }
>> vecX.push_back(argv[3]);
>>
>> printf("\n");
>> return 0;
>> };
>> [/code]
>> Now this doesn't seem object oriented ? 
>> Nor do I understand how I would access this vector from other classes ? 
>> And I don't know how this vector which I created can be considered part of 
>> the class ? it seems to me to only have page scope. 
>> Any advice on how to make my vector object oriented or accessible by other 
>> classes ? 
>>
>> Thanks in Advance 
>> Stephen 
>>
>>
>> ---------------------------------
>> Pinpoint customers who are looking for what you sell. 
>>
>> 
>
> 

-- 
Regards,
Igor Mironchick,
Intervale ©
#ICQ 492-597-570


-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------



       
---------------------------------
Boardwalk for $500? In 2007? Ha! 
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.

Reply via email to