How to Get Column Names in SQLite Database

Vaibhav Vaibhav Feb 02, 2024
  1. Get Column Names Using PRAGMA
  2. Get Column Names Using PRAGMA_TABLE_INFO
How to Get Column Names in SQLite Database

SQLite is a database management system or DBMS written in the C programming language. It is a library that creates a single-file database in the specified directory and stores all the databases and tables inside that file.

A database schema refers to the logical representation of a database. Simply put, it refers to the overall structure of a database; what tables are there, what are the attributes of those tables, which table is linked to which table, what is the cardinality between any two connected tables, etc.

A database table schema refers to the structure of a table. It includes details about the attributes, types, default values, constraints, etc.

When working with databases and tables, we often need to retrieve columns and their metadata for various purposes. This article will teach us how to fetch column details in the SQLite database.

Get Column Names Using PRAGMA

PRAGMA is a SQL extension specific to the SQLite database. It enables users to query the internal data of a database.

Using this command, we can fetch the column names for any table.

PRAGMA table_info(table_name);

This SQL command returns a table that shows all the columns in the table and details their type, primary key status, NOT NULL status, and default value.

Get Column Names Using PRAGMA_TABLE_INFO

Apart from the PRAGMA command mentioned above, we can use a PRAGMA method, namely, PRAGMA_TABLE_INFO, to fetch the names of all the columns of a table. The following is the syntax for the command.

SELECT name FROM PRAGMA_TABLE_INFO("table_name");

Unlike the last command, this only returns the names of all the columns. Additionally, this command only works on a local database but not with attached schemas.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - SQLite Column