tags:

views:

146

answers:

2
void max_min(sqlite3 *db)
{
    //call back*********
    int i, ncols;
    sqlite3_stmt *stmt;
    char *sql;
    const char *tail;
    char *zErrMsg = 0;
    int rc;
    //******************

    //min/max variables
    char min[20];
    char max[20];




    //we want only the min and max value of this table
    sql = "SELECT MIN(Start),MAX(End)FROM GMTI;"; //doesn't extract but works in GUI tool?

    //sql = "SELECT * FROM GMTI WHERE Start<16;"; //works?

    rc = sqlite3_prepare(db, sql, strlen(sql), &stmt, &tail);
     if(rc != SQLITE_OK){
      fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
     }

    rc = sqlite3_step(stmt);
    ncols = sqlite3_column_count(stmt);
    printf("Number of columns: %d and row status: %d", ncols, rc);
    while(rc == SQLITE_ROW){
     for(i=0; i<ncols; i++){
           if (strncmp("Start", sqlite3_column_name(stmt,i), strlen("Start")) == 0)
         {
            strncpy(min, sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
            strcpy(min + strlen(sqlite3_column_text(stmt,i)), "\0");
         printf("min is: %s\n", min);
         printf("<br>");
         }  
         if (strncmp("End", sqlite3_column_name(stmt,i), strlen("End")) == 0)
         {
            strncpy(max, sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i)));
            strcpy(max + strlen(sqlite3_column_text(stmt,i)), "\0");
         printf("max: %s\n", max);
         printf("<br>");
         }


      }//end for 

     fprintf(stderr, "\n");
    rc = sqlite3_step(stmt);
    }//end while

    sqlite3_finalize(stmt);

}

When I use the sql query above with the MIN and MAX functions, nothing prints out for min and max. If I use one of the other statements commented out there, it works as expected, printing the selected query.
What is different about the max and min in the query, that it can't extract the values? Is it not in table format?
Also, I tested the problem query string in an SqLite database browser on the same database and it works, displaying the two - min and max values.

Any suggestions would be greatly appreciated.

Thanks.

+2  A: 

Try printing out the returned column names. Maybe they don't equal "Start" and "End" as you assumed?

Paul Tomblin
I can print out the data under the start and end by simply using the other Select * from table. It is when I specify the MIN and MAX there is an issue...
Tommy
That's my point - maybe the column names are "MIN(Start)" and "MAX(End)" rather than "Start" and "End" as you've assumed.
Paul Tomblin
+1: Column names not what were assumed. Must collect facts.
S.Lott
How does the same string work in the sqlite dabatse browser on the same database?
Tommy
Because in the browser you don't have a big chunk of C code that checks the column names and will only process them if the column names are "Start" and "End".
Paul Tomblin
A: 

Maybe you should add a space between MAX(End) and FROM ?

tuinstoel