SQLite Forum

getting list of columns
Login

getting list of columns

(1) By anonymous on 2021-12-18 01:44:07 [link] [source]

We are using this command to get column names

pragma table_info("stores")

and we get

0|id|INTEGER|0||1
1|email|TEXT|0||0
2|title|TEXT|0||0
3|description|TEXT|0||0
4|location |TEXT|0||0
5|zip |TEXT|0||0

is there a command we can get just the names of the columns so we don't have to parse it ?

something like

id|email|title|description|location|zip

(2) By Larry Brasfield (larrybr) on 2021-12-18 03:03:39 in reply to 1 [link] [source]

After a query such as "SELECT * FROM SomeTable" has been submitted sqlite3_prepare() and its sibs, with a SQLITE_OK return, the sqlite3_column{count,name,name16} APIs can be used to get those column names.

At this time, the sqlite3 CLI declines to show column names for empty result sets. So if "a command" means a dot command given to that tool, the answer is "No" at this time.

(3) By Igor Tandetnik (itandetnik) on 2021-12-18 04:43:20 in reply to 1 [source]

Something along these lines:

select group_concat(name, '|')
from pragma_table_info('stores')

(4) By AlexJ (CompuRoot) on 2021-12-18 07:20:40 in reply to 1 [link] [source]

echo 'pragma table_info("stores");' | sqlite3 yourDatabase.sqlite  | awk -F\| '{gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2); printf "%s|", $2}'

(5) By anonymous on 2021-12-18 16:08:24 in reply to 1 [link] [source]

What about

select name from pragma_table_info('stores');

(6) By anonymous on 2021-12-18 17:13:46 in reply to 3 [link] [source]

it worked perfectly!! thank you!

(7) By anonymous on 2021-12-18 17:15:21 in reply to 5 [link] [source]

it worked perfectly !!! thanks a lot!!!

(8) By ET (EricTsau) on 2021-12-19 08:14:28 in reply to 1 [link] [source]

Backlink to this question on stackoverflow: How to get a list of column names on Sqlite3 database?.

(9) By skywalk on 2021-12-19 16:09:19 in reply to 1 [link] [source]

Cool to learn of the 'name' field, but why do you not need datatype, default value, notnull, etc of the columns? It is necessary to process data in/out of that table.