Tags
SQLite
Asked 7 years ago
13 Feb 2017
Views 1158
jessica

jessica posted

how to get list of columns of table by Query in SQLite ?

select Query result data of given table

select * from students

but is there any query which result column list of given table in SQLite ?
i want to list columns of "students" table , by Query

suppose MySQL have

show columns from tablename


is there any way we can get all column list of table in SQLite ?
Phpworker

Phpworker
answered Apr 24 '23 00:00

To retrieve a list of columns for a table in SQLite using a query, you can use the PRAGMA statement with the table_info argument. This can be done in any programming language that supports SQLite.

Here's an example using PHP and the PDO extension:



// Establish a connection to the SQLite database
$db = new PDO('sqlite:example.db');

// Prepare the PRAGMA statement to retrieve table information
$statement = $db->prepare('PRAGMA table_info(table_name)');

// Execute the statement
$statement->execute();

// Retrieve the table information as an array of associative arrays
$table_info = $statement->fetchAll(PDO::FETCH_ASSOC);

// Iterate through the table information to extract the column names
foreach ($table_info as $column_info) {
    $column_names[] = $column_info['name'];

}

// Print the column names
print_r($column_names);
In the above example, replace example .db with the name of the SQLite database file you're using, and replace table_name with the name of the table you want to retrieve the columns for.

The table_info result is fetched as an array of associative arrays, where each array represents a column in the table. The name of the column is extracted from each associative array and stored in a new array called $column_names . Finally, the column names are printed using print_r().
Post Answer