c++databasesqlitebetter-sqlite3

Find number of columns in a table | Sqlite3


I am using cpp and -lsqlite3 to handle some data. I just wanted to ask about a query that will return the number of columns of a specific table in sqlite. Is it possible to find out?


Solution

  • With sufficiently recent SQLite version:

    select count(*)
    from pragma_table_info('tableName')
    

    If it doesn't have to be a SQL query but could be an API call, then a) prepare a query select * from tableName (just prepare, you don't need to actually execute it), then call sqlite3_column_count on the resulting statement handle.