sqlite

How to format the table output in sqllite


If I do this in sqlite3 shell it works perfectly but I want to do this same in my C++ code. If I use a callback function to print the columns then I don't know how to pass that ".mode" and ".header" arguments in C++ to print the table in a below-formatted manner.

below code from this link

sqlite> .mode column
sqlite> .headers on
sqlite> select * from foo;
bar         baz         baf
----------  ----------  ----------
234         kshitiz     dba.se

Solution

  • .mode and .headers are commands specific to the sqlite executable, not SQL commands you can use elsewhere. So you have to reproduce the behavior in code, which fortunately is not difficult.

    To fetch column names from a table you can read from sqlite_master but there are other ways. Check out this post for example: How to read metadata from Sqlite database