Fetching Data
In this section, we have discussed how to create a table and how to add new rows in the database. Fetching the data from records is simple as inserting them. The execute method uses the SQL command of getting all the data from the table using “Select * from table_name” and all the table data can be fetched in an object in the form of a list of lists.
Example: Reading Data from sqlite3 table using Python
Python
# importing the module import sqlite3 # connect with the myTable database connection = sqlite3.connect( "gfg.db" ) # cursor object crsr = connection.cursor() # execute the command to fetch all the data from the table emp crsr.execute( "SELECT * FROM emp" ) # store all the fetched data in the ans variable ans = crsr.fetchall() # Since we have already selected all the data entries # using the "SELECT *" SQL command and stored them in # the ans variable, all we need to do now is to print # out the ans variable for i in ans: print (i) |
Output:
Note: It should be noted that the database file that will be created will be in the same folder as that of the python file. If we wish to change the path of the file, change the path while opening the file.
SQL using Python
In this article, integrating SQLite3 with Python is discussed. Here we will discuss all the CRUD operations on the SQLite3 database using Python. CRUD contains four major operations –
Note: This needs a basic understanding of SQL.
Here, we are going to connect SQLite with Python. Python has a native library for SQLite3 called sqlite3. Let us explain how it works.