In this article, we will discuss how we can show all columns of a table in the SQLite database from Python using the sqlite3 module.
Approach:
- Connect to a database using the connect() method.
- Create a cursor object and use that cursor object created to execute queries in order to create a table and insert values into it.
- Use the description keyword of the cursor object to get the column names. The description keyword specifies only the columns of a table in a two-dimensional tuple consisting of none values and the column names only.
data=cursor.execute('''SELECT * FROM table_name''')
print(data.description)
The above code displays all the columns of a given table in a two-dimensional tuple.
- Display the data in the table by executing the below query using the cursor object.
SELECT * FROM table_name
- Finally, commit the changes in the database and close the connection.
Below is the Implementation:
Creating the table
In the below program we connect to a database named gfg.db, then we create an EMPLOYEE table and insert values into it. Finally, we commit the changes in the database and terminate the connection.
Python3
import sqlite3
conn = sqlite3.connect( 'gfg1.db' )
cursor = conn.cursor()
table =
cursor.execute(table)
print ( 'Table Created!' )
cursor.execute(
)
cursor.execute(
)
cursor.execute(
)
cursor.execute(
)
cursor.execute(
)
print ( 'Data inserted into the table' )
conn.commit()
conn.close()
|
Output:
Table Created!
Data inserted into the table
Retrieving columns from the table:
Now as we have already created a table and inserted values into the table in a database. We will connect to the previous database where the EMPLOYEE table is created. Then we will first display all the columns and then the data values in the column.
Python3
import sqlite3
conn = sqlite3.connect( 'gfg.db' )
cursor = conn.cursor()
print ( '\nColumns in EMPLOYEE table:' )
data = cursor.execute( )
for column in data.description:
print (column[ 0 ])
print ( '\nData in EMPLOYEE table:' )
data = cursor.execute( )
for row in data:
print (row)
conn.commit()
conn.close()
|
Output:

SQLite:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!