The original example's an accident waiting to happen. You've hard
coded the results to 3 but, not limited the number of loops in the
while loop so, you could easily crash/corrupt the stack. You'll crash
if you have less than 3 results too. What happens if you only get one
result from the while loop? You're going to induce a crash in the
printf, even with the strdup.

I'd pull the while loop out and verify that you can make it work for
one item. Then expand it to work with more items.


const char *dbname = "test.db";
const char *sql = "SELECT * FROM table1";
const unsigned char *my_array[3];
int i=0;;


     sqlite3_open(dbname, &db);
    sqlite3_prepare_v2(db, sql, -1, &pStat, 0);

     while(sqlite3_step(pStat) == SQLITE_ROW && (i < 3))
     {
       const char* pszData = sqlite3_column_text(pStat, 0);
       if( pszData )
       {
            my_array[i] = strdup(pszData)
            printf ("%s\n",pszData); // ok
             i++;
       }
     }

     
     //
     // Only printf what you actually read in
     //
     int Loop = 0;
     for (Loop  = 0; Loop < i; Loop++);
     {
       printf ("%s\n", my_array[Loop]); // error
     }



Tuesday, October 9, 2007, 10:50:57 AM, you wrote:

>> 
>> 
>> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: Hello,
>> 
>> I got an error when I try to read some data outside the while{}, inside the 
>> while{} it's ok, an idea ?
>> test.db have just one "table1" and a "field1" with values "one", "two", 
>> "three".
>> 
>> 
>> #include 
>> #include 
>> 
>> int main(void)
>> {
>>     sqlite3 *db;
>>     sqlite3_stmt *pStat;
>> 
>>     const char *dbname = "test.db";
>>     const char *sql = "SELECT * FROM table1";
>>     const unsigned char *my_array[3];
>>     int i=0;;
>>   
>>  
>>     sqlite3_open(dbname, &db);
>>     sqlite3_prepare_v2(db, sql, -1, &pStat, 0);
>>     
>>     while(sqlite3_step(pStat) == SQLITE_ROW)
>>     {
>>  my_array[i] = sqlite3_column_text(pStat, 0);
>>      printf ("%s\n",my_array[i]); // ok
>> 
>>  i++;
>>     }
>> 
>>     for (i = 0; i<3; i++);{
>>  printf ("%s\n", my_array[i]); // error
>>     }
>>
>>     sqlite3_finalize(pStat);
>>     sqlite3_close(db);
>>  
>>     return 0;
>> }
>> 
>> 
>> Fred.
>> 
>> 
>> -----------------------------------------------------------------------------
>> To unsubscribe, send email to [EMAIL PROTECTED]
>> -----------------------------------------------------------------------------
>> 
>> 
>> 

fff> Ken a écrit :
>> You need to make a copy of the str instead of just capturing a pointer 
>> reference.
>> 
>> try:
>>       my_array[i] = strdup(sqlite3_column_text(pStat, 0));
>> 

fff> Its same with strdup()


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



-- 
Best regards,
 Teg                            mailto:[EMAIL PROTECTED]


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

Reply via email to