Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to pass the array where you want to store the data in as the 1st argument to the callback as shown in sqlite3 <a href="http://sqlite.org/c3ref/exec.html">docs</a></p> <pre><code>int sqlite3_exec( sqlite3*, /* An open database */ const char *sql, /* SQL to be evaluated */ int (*callback)(void*,int,char**,char**), /* Callback function */ void *, /* 1st argument to callback */ char **errmsg /* Error msg written here */ ); </code></pre> <p>Here's a contrived example based on your code with some tidy up:</p> <pre><code>#include &lt;vector&gt; #include &lt;string&gt; #include &lt;iostream&gt; #include "sqlite3.h" using namespace std; sqlite3* db; using Record = std::vector&lt;std::string&gt;; using Records = std::vector&lt;Record&gt;; int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names) { Records* records = static_cast&lt;Records*&gt;(p_data); try { records-&gt;emplace_back(p_fields, p_fields + num_fields); } catch (...) { // abort select on failure, don't let exception propogate thru sqlite3 call-stack return 1; } return 0; } Records select_stmt(const char* stmt) { Records records; char *errmsg; int ret = sqlite3_exec(db, stmt, select_callback, &amp;records, &amp;errmsg); if (ret != SQLITE_OK) { std::cerr &lt;&lt; "Error in select statement " &lt;&lt; stmt &lt;&lt; "[" &lt;&lt; errmsg &lt;&lt; "]\n"; } else { std::cerr &lt;&lt; records.size() &lt;&lt; " records returned.\n"; } return records; } void sql_stmt(const char* stmt) { char *errmsg; int ret = sqlite3_exec(db, stmt, 0, 0, &amp;errmsg); if (ret != SQLITE_OK) { std::cerr &lt;&lt; "Error in select statement " &lt;&lt; stmt &lt;&lt; "[" &lt;&lt; errmsg &lt;&lt; "]\n"; } } int main() { if (sqlite3_open("test.db", &amp;db) != SQLITE_OK) { std::cerr &lt;&lt; "Could not open database.\n"; return 1; } Records records = select_stmt("SELECT * FROM test"); sqlite3_close(db); for (auto&amp; record : records) { // do something with your records } return 0; } </code></pre> <p>Your question is tag c++, so I've removed your use of the standard c api calls in favour of standard c++.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload