'How to get the type of a column result of a SELECT query in SQLite c++?

I'm working in c++ with SQLite, where I need to do some SELECT over a database. The problem is that the SELECT statement is one of the types

SELECT * FROM TABLE;  //Unknown data type of columns
SELECT A,B FROM TABLE;//Unknown data type of columns

Where I don't know the types of the columns, but I need to store the retrieved values in corresponding c++ variables with a specific type. So the problem is that I don't know how to get the column data type of the result set given by the SQL SELECT statement after run the sqlite3_step to get the first result set.

In the code I have

//Prepare the statement
rc=sqlite3_prepare_v2
    (
        connection.database_handle,/* Database handle */
        sql_command.c_str(),       /* SQL statement, UTF-8 encoded */
        sql_command.size(),        /* Maximum length of zSql in bytes. */
        &sql_statement_handle,     /* OUT: Statement handle */
        NULL                       /* OUT: Pointer to unused portion of zSql */
    );
if(rc!=SQLITE_OK)
{
    return -1;
}

To prepare the select command (in this case "SELECT * FROM TABLE;"), then I have the following to know the total columns in the result set

//Get the total number of columns
stmt_columns=sqlite3_column_count(sql_statement_handle);
if(stmt_columns<=0)
{
    return -1;
}
sqlite_column_types=new int[stmt_columns];

After that iterating over the stmt_columns I have

sqlite_column_type=sqlite3_column_type(sql_statement_handle,i);
sqlite_column_types[i]=sqlite_column_type;

In order to retrieve the columns types. But the result is always the NULL data type which has no sense in this case. I tried running first the SQLite function sqlite3_step and then running the function sqlite3_column_type to get the columns type but the behavior is the same. So the question is how to know the columns data type of a SELECT command result set where I don't know in advance the columns types of the table or the query?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source