[sqlite] Re: Reading error outside the while

2007-10-10 Thread Igor Tandetnik

[EMAIL PROTECTED] wrote:

   while(sqlite3_step(pStat) != SQLITE_DONE)
   {
  switch (sqlite3_step(pStat)) {


You call sqlite3_step twice on every iteration, which means you are only 
looking at every other row. That's probably not what you wanted.



 case SQLITE_ROW:
 /*Get each column*/
 for (i = 0; i < sqlite3_column_count(pStat); i++)
 {
  switch (sqlite3_column_type(pStat,0))
   {
 case SQLITE_TEXT:
   printf("%s ", sqlite3_column_text(pStat,0));
 break;
   }
 break;
   }


Why do you run the 'for' loop, if you unconditionally break out of it on 
the very first iteration anyway?


Igor Tandetnik 



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



[sqlite] Re: Reading error outside the while

2007-10-09 Thread Igor Tandetnik

[EMAIL PROTECTED] wrote:

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

   while(sqlite3_step(pStat) == SQLITE_ROW)
   {
row = (char *)sqlite3_column_text(pStat, 0);
my_array[i] = malloc( sizeof row *  sizeof *my_array[i]);

memcpy (my_array[i], row, sizeof row * sizeof *my_array[i]);


sizeof(row) == sizeof(char*) == 4 (assuming a 32-bit system). In any 
case, sizeof(row) is completely unrelated to the length of the string 
'row' might be pointing to. See sqlite3_column_bytes.



   printf ("%s\n",my_array[i]); // ok


Are you sure the output is correct here? It's only possible if your 
strings happen to be short enough to fit in 4 bytes.




i++;
   }

for (i = 0; i<3; i++);{
printf ("%s\n", my_array[i]); //error


Remove the semicolon between the closing paren and the opening brace. 
Your code as written is equivalent to this:


for (i = 0; i < 3; i++) {
   // loop body is empty
}

// At this point, i == 3
printf ("%s\n", my_array[3]);  // index out of bounds


free (my_array);


You haven't allocated my_array with malloc, so you shouldn't deallocate 
it with free. Your program will likely crash on this statement.


You did allocate my_array[0], my_array[1] and my_array[2] with malloc, 
so you should deallocate these three pointers with free. Do you 
understand the difference?



With all due respect, I suggest you spend some time with your favorite C 
textbook. The problems in your code have very little to do with SQLite, 
and a lot to do with misusing C language.


Igor Tandetnik 



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