How to Show all Columns in the SQLite Database using Python ?
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.
- 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.
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course