I’m calling
sqlite3_exec(db, "SELECT a,b,Z FROM t1 WHERE t1.id = 1", func_ptr, arg_ptr)
to collect the row content and the column names. I’m trying to create a generic helper function that will take any SELECT command and construct a vector<vector<string>> with the first row containing the column names (and rest containing the contents of the rows).
But this fails to capture the column names when there are no rows to show since func_ptr is only called with one or more rows to display.
How can I get the column names when SELECT returns zero rows?
EDIT: I would prefer not to create a level of indirection by creating a table and then calling SELECT on that table. Thanks.
EDIT: I would only like to collect what the SELECT statement contains.
You can use
PRAGMA table_info(tablename);to get a result set that includes column name and type for the given table.Call the pragma as you would a normal query (i.e. with
sqlite3_execfor instance).If you need this information for a SQL statement you didn’t generate yourself that contains a column selection, you need to use a prepared statement (you should be doing that anyway). Then you can use
sqlite3_column_countandsqlite3_column_nameto get the information you’re after.Example usage:
You can adapt this to store the column names in a
std::vector.