title: Retrieve Sqlite table data in C++
body: <p>I have the code as below. I am trying to simply get data from a table i have already created. How do i get the data in the array p_fields that is defined in the callback function into a variable in main. Can i define a char ** array in main and copy the data somehow into it in the callback function?</p>
  
  <p>Thanks very much,
  Shyam.</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include "sqlite3.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  sqlite3* db;
  
  int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names) {
  
    int i;
  
    for(i=0; i &lt; num_fields; i++) {
      if (p_fields[i]) {
        printf("%20s", p_fields[i]);
      }
      else {
        printf("%20s", " ");
      }
    }
    printf("\n");
    return 0;
  }
  
  void select_stmt(const char* stmt) {
    char *errmsg;
    int   ret;
    int   nrecs = 0;
    float var;
  
    ret = sqlite3_exec(db, stmt, select_callback, &amp;nrecs, &amp;errmsg);
  
    if(ret!=SQLITE_OK) {
      printf("Error in select statement %s [%s].\n", stmt, errmsg);
    }
    else {
      printf("\n   %d records returned.\n", nrecs);
    }
  
    cout&lt;&lt; ret &lt;&lt; endl;
  }
  
  void sql_stmt(const char* stmt) {
    char *errmsg;
    int   ret;
  
    ret = sqlite3_exec(db, stmt, 0, 0, &amp;errmsg);
  
    if(ret != SQLITE_OK) {
      printf("Error in statement: %s [%s].\n", stmt, errmsg);
    }
  }
  
  int main() {
  
    sqlite3_open("Flamelet.db", &amp;db);
  
    if(db == 0) {
      printf("\nCould not open database.");
      return 1;
    }
  
    sqlite3_stmt *stmt;
    select_stmt("SELECT density from Ftable where PROG=10.0");
  
    sqlite3_close(db);
    return 0;
  }
  </code></pre>