Is there a way to get a list of column names in sqlite?
You can use sqlite3 and pep-249
import sqlite3
connection = sqlite3.connect('~/foo.sqlite')
cursor = connection.execute('select * from bar')
cursor.description is description of columns
names = list(map(lambda x: x[0], cursor.description))
Alternatively you could use a list comprehension:
names = [description[0] for description in cursor.description]
How can I get the list of a columns in a table for a SQLite database?
What you're looking for is called the data dictionary. In sqlite a list of all tables can be found by querying sqlite_master table (or view?)
sqlite> create table people (first_name varchar, last_name varchar, email_address varchar);
sqlite> select * from sqlite_master;
table|people|people|2|CREATE TABLE people (first_name varchar, last_name varchar, email_address varchar)
To get column information you can use the pragma table_info(table_name)
statement:
sqlite> pragma table_info(people);
0|first_name|varchar|0||0
1|last_name|varchar|0||0
2|email_address|varchar|0||0
For more information on the pragma statements, see the documentation.
How to find information on all columns in a SQLite database?
You can query the table sqlite_master
to get all the tables of the database and then use the table valued function pragma_table_info()
to get for each table its columns:
WITH all_tables AS (SELECT name FROM sqlite_master WHERE type = 'table')
SELECT at.name table_name, pti.*
FROM all_tables at INNER JOIN pragma_table_info(at.name) pti
ORDER BY table_name;
Retrieve column names from sqlite3 database with php
Refer to SQLite.org - PRAGMA Statements:
PRAGMA table_info(sqlite_master);
To fit it into your PHP implementation:
<?php // Display all sqlite tables
$db = new SQLite3('data.db');
$tablesquery = $db->query("PRAGMA table_info(sqlite_master);");
while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
echo $table['name'] . '<br />';
} ?>
How to get column names with query data in sqlite3?
Yes, after logged into sql prompt mode, execute the following each at a time:
.header on
.mode column
How to get list of column names of a sqlite db file
use the pragma table_info(spamtable)
command. The table names will be index 1 of the returned tuples.
Related Topics
Directing Print Output to a .Txt File
Python: How to Run Eval() in the Local Scope of a Function
Rect Collision with List of Rects
How to Trace the Path in a Breadth-First Search
Does Flask Support Regular Expressions in Its Url Routing
How to Create an Empty Array and Then Append to It in Numpy
How to Schedule a Function to Run Every Hour on Flask
Finding Elements Not in a List
For Loops and Iterating Through Lists
How to Create a Slug in Django
Python Ungzipping Stream of Bytes
Select Multiple Ranges of Columns in Pandas Dataframe
Pandas Read CSV File with Float Values Results in Weird Rounding and Decimal Digits
Multiprocessing VS Multithreading VS Asyncio in Python 3
Python 3 Replacement for Deprecated Compiler.Ast Flatten Function