Retrieving the data

Now, we are going to retrieve the data from the table. For that, create a retrieve_data.py script and write the following content in it:

import sqlite3

con_obj = sqlite3.connect('test.db')
cur_obj = con_obj.execute("SELECT title, author from books")
f
or row in cur_obj:
print ("Title = ", row[0])
print ("Author = ", row[1], " ")

con_obj.close()

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 retrieve_data.py

Output:
Title = Pride and Prejudice
Author = Jane Austen

Title = Harry Potter
Author = J.K Rowling

Title = The Lord of the Rings
Author = J. R. R. Tolkien

Title = Murder on the Orient Express
Author = Agatha Christie

Title = A Study in Scarlet
Author = Arthur Conan Doyle

In the preceding example, we imported the sqlite3 module. Next, we connected with our test.db database . To retrieve the data, we used the select statement. And, last, we printed the retrieved data.

You can also retrieve the data in the sqlite3 console. For that, start the SQLite console first and then retrieve the data as follows:

student@ubuntu:~/work/sqlite3_testing$ sqlite3 test.db

Output:
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite>
sqlite> select * from books;
Pride and Prejudice|Jane Austen
Harry Potter|J.K Rowling
The Lord of the Rings|J. R. R. Tolkien
Murder on the Orient Express|Agatha Christie
A Study in Scarlet|Arthur Conan Doyle
sqlite>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset