1

I want to detect column data types of any SELECT query in SQLite.

In the C API, there is const char *sqlite3_column_decltype(sqlite3_stmt*,int) for this purpose. But that only works for columns in a real table. Expressions, such as LOWER('ABC'), or columns from queries like PRAGMA foreign_key_list("mytable"), always return null here.

I know there is also typeof(col), but I don't have control over the fired SQL, so I need a way to extract the data type out of the prepared statement.

Anse
  • 1,573
  • 12
  • 27

1 Answers1

2

You're looking for sqlite3_column_type():

The sqlite3_column_type() routine returns the datatype code for the initial data type of the result column. The returned value is one of SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, or SQLITE_NULL. The return value of sqlite3_column_type() can be used to decide which of the first six interface should be used to extract the column value.

And remember that in sqlite, type is for the most part associated with value, not column - different rows can have different types stored in the same column.

Shawn
  • 47,241
  • 3
  • 26
  • 60
  • That works like a charm, thanks! I read that documentation page several times but oversaw this special one below the others. One needs to get used to these value based data types. But I suppose there were myriads of discussions with regard to that, so I'll keep calm :) – Anse Jan 15 '20 at 21:53