title: SQLite in C. sqlite3_exec: parameter set in callback function prints incomplete data when called in main()
body: <p>I am trying to achieve something very basic. But unfortunately I am unable to get the expected result. </p>
  
  <p>I have a sqlite table named <strong>emp_info</strong>having 9 employee records in following fields:</p>
  
  <p><strong>SR_NO[0], NAME[1], AGE[2], SEX[3], ADDRESS[4], EMPID[5], CARDID[6] and SALARY[7]</strong>.</p>
  
  <p>To access the table and fetch the employee record as per sql query, I have following program in C:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sqlite3.h&gt; 
  #include &lt;string.h&gt;
  
  static int callback(void *param, int argc, char *argv[], char **azColName){
      int i=0;
  
      if(argc == 0) return 0;
      char **res = (char **)param;
      *res = (char *)realloc(*res, sizeof(*res));
  
    //for(i=0; i&lt;argc; i++){
    //printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  
      strcpy(*res, argv[1], sizeof(*res));
      printf("%s\n", *res);
   // }
  return 0;
  }
  
  int main(int argc, char* argv[])
  {
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;
  char *sql;
  char *param;
  
  rc = sqlite3_open("/DBsqlite3/foreign_key.db", &amp;db);
  
      if( rc )
      {
         fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
         exit(0);
      }
      else
      {
         fprintf(stderr, "\nOpened database successfully\n\n");
      }
  
  sql="select * from emp_info;"
  
  rc = sqlite3_exec(db, sql, callback, &amp;param, &amp;zErrMsg);
  
      if( rc != SQLITE_OK )
      {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
      }
      else
      {
         fprintf(stdout, "Operation done successfully\n\n");
         printf("the value of data is %s \n\n", param);
      }
  
  
  free(param);
  sqlite3_close(db);
  return 0;
  }
  </code></pre>
  
  <p>The output is as follows:
  `
      Opened database successfully</p>
  
  <pre><code>AAA
  BBB
  CCC
  DDD
  EEE 
  FFF
  GGG
  HHH
  III
  Operation done successfully
  
  the value of data is III
  </code></pre>
  
  <p>I expected that all the AAA,BBB,CCC,.... will get stored in <strong>param</strong> and will get printed. But that's not happening. <strong>param</strong> takes only the last record only.</p>
  
  <p>Why is this happening ? I suspect strcpy() is creating the problem.</p>
  
  <p>Kindly help.</p>
  
  <p>Apart from this, my real aim is to get all the valid employee record (as per sql query) to get stored in <strong>param</strong> and it should display all the fields(SR_NO[0], NAME[1], AGE[2], SEX[3], ADDRESS[4], EMPID[5], CARDID[6] and SALARY[7]). say for example if i provide following query to <strong>sql</strong></p>
  
  <pre><code>sql="SELECT * FROM emp_info WHERE AGE&gt;40";
  </code></pre>
  
  <p>then the <strong>param</strong> should display result as follow</p>
  
  <pre><code>7           GGG         41          MALE        G1G2G3G4    307         7777GGG     77777     
  
  8           HHH         42          FEMALE      H1H2H3H4    308         8888HHH     88888
  
  9           III         43          FEMALE      I1I2I3I4    309         9999IIII    99999
  </code></pre>
  
  <p>And not just a column.How can I achieve this ?</p>
  
  <p>Thank you in advance.</p>