We can update the data from our table using the update statement. Now, we are going to see an example of updating data. For that, create a update_data.py script and write the following content in it:
import sqlite3
con_obj = sqlite3.connect("test.db")
with con_obj:
cur_obj = con_obj.cursor()
sql = """
UPDATE books
SET author = 'John Smith'
WHERE author = 'J.K Rowling'
"""
cur_obj.execute(sql)
print("Data updated Successfully !!")
Run the script and you will get the following output:
student@ubuntu:~/work $ python3 update_data.py
Output:
Data updated Successfully !!
Now, to check that the data is actually updated or not, you can run retrieve_data.py, or else you can go to the SQLite console and run select * from books;. You will get the updated output as follows:
By running retrieve_data.py:
Output:
student@ubuntu:~/work$ python3 retrieve_data.py
Title = Pride and Prejudice
Author = Jane Austen
Title = Harry Potter
Author = John Smith
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
Checking on SQLite console:
Output:
student@ubuntu:~/work$ sqlite3 test.db
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|John Smith
The Lord of the Rings|J. R. R. Tolkien
Murder on the Orient Express|Agatha Christie
A Study in Scarlet|Arthur Conan Doyle
sqlite>