Inserting the data

Now, we will insert the data into our table. For that, we will create a insert_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()
cur_obj.execute("INSERT INTO books VALUES ('Pride and Prejudice', 'Jane Austen')")
cur_obj.execute("INSERT INTO books VALUES ('Harry Potter', 'J.K Rowling')")
cur_obj.execute("INSERT INTO books VALUES ('The Lord of the Rings', 'J. R. R. Tolkien')")
cur_obj.execute("INSERT INTO books VALUES ('Murder on the Orient Express', 'Agatha Christie')")
cur_obj.execute("INSERT INTO books VALUES ('A Study in Scarlet', 'Arthur Conan Doyle')")
con_obj.commit()

print("Data inserted Successfully !!")

Run the script and you will get the following output:

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

Output:
Data inserted Successfully !!

In the preceding example, we inserted some data into our table. For that, we used insert in the SQL statement. By using commit(), we are telling the database to save all the current transactions.

..................Content has been hidden....................

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