John Stanton a écrit :
[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 <stdio.h>
#include <sqlite3.h>

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.


Your program is wrongly structured.  Try this layout:

    sqlite3_prepare_V2

    while (TRUE) {
      rc = sqlite3_step
      switch (rc) {
        case SQLITE_ROW:
          /*Get each column*/
          for (count = 0; count > sqlite3_column_count; count++) {
            switch (sqlite3_column_type) {
              case SQLITE_TEXT:
                pt = sqlite3_column_text
                /*Move text into your output*/
                sprintf(outval, "%s", pt);  /*Or some other move.*/
                break;
              case SQLITE_INTEGER:
                outnbr = sqlite3_column_int;
or
                sprintf(outval, "%d", sqlite_column_int(..));
                break;
              ....
              add other types
             }
            }
          break;
        case SQLITE_DONE:
          sqlite3_finalize
          return from function
        case SQLITE_BUSY:
          /*Handle BUSY condition.*/
          break;
        default:
          /*Handle error condition.*/
          break;
       } /*switch*/
      }  /*while*/

Now you can handle errors, busy conditions and the return of differring
types from Sqlite. When you get a pointer to a text value from Sqlite it is the programmer's responsibility to move data from that pointer into data in your program.

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



Hello John,

Thanks for reply,

I just tested with your code that seem to be more useful, but something is 
wrong :

#include <stdio.h>
#include <sqlite3.h>


int main(void)
{
        sqlite3 *db;
   sqlite3_stmt *pStat;
   const gchar *dbname = "test.db";
   int i;
        sqlite3_open(dbname, &db);
   sqlite3_prepare_v2(db, "SELECT * FROM TABLE1", -1, &pStat, 0);

   while(sqlite3_step(pStat) != SQLITE_DONE)
   {
                switch (sqlite3_step(pStat)) {
         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;
}
           }    
   }
                
   sqlite3_close(db);
        
        return 0;
}


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

Reply via email to