Python 3!! How to Use SQLITE 3 Module

 

Hello everyone! In this article, we are going to see some basics of SQLite, easy steps to connect SQLite with python and create the database with it. After that, we will insert and retrieve data from the database.Let’s start!


Basics Of SQLite For Python :-

  • It is an embedded relational database engine.
  • It is a C library that provides a lightweight disk-based database that doesn’t require a separate server process, which means it is a serverless database.
  • It is small, fast, self-contained, has high reliability, and is fully featured.
  • The database file that you create resides in your computer only.
  • The SQLite database can be accessed using the DB Browser.

Connecting SQLite Database With Python :-

Python

In the above code, you can see that we have imported the sqlite3 to access the functionality of that module. Then we have created the connection object using sqlite3.connect() to interact with the SQLite database. In the connect() method, we have to give the path of the database which you want to access and if the database does not exist then it will create a database.

 After that, we have created the cursor object using the connection. cursor() to execute statements to communicate with the database. Using the methods of connection. cursor() we can execute sqlite3 statements, fetch data from the result sets, and call procedures.

Python

In the next step we have created a variable named query to store the SQL statement to create a table and passed that query in cursor.execute(query) or we can directly pass that statement in the cursor.execute() method. The execute() method of the cursor executes the SQL statement that has been stored in the variable query. For executing multiple statements together we can use cursor.executescript().

After executing we need to commit the connection to save the changes or the work we have done using connection.commit().

Python

Always make a practice to write code in try block. If any error raises an error, the except block will be executed.

The finally block lets you execute code, regardless of the result of the try and except blocks. So try to make a habit to close the cursor and the connection in the finally block.


Insert And Retrieve Data From Database 

Python



In the above code, we have inserted and retrieved data from the database named demo1. First, we have made a connection with the database. Then we have created a table named student1. All the insert statements are executed using cursor.executescript().

Once the data is inserted, now we have to retrieve the data. For retrieving the data, we have to store the select statement in one variable named query and execute using cursor.execute(). Now the retrieved data is stored in the cursor object. For accessing the data we have to execute a for loop on the cursor and print it. After that, we have to close the cursor and the connection.

If you like this article, please share it with your family and friends. Also keep visiting SwagBlogger for more new updates and tips.

0 Comments

disawar satta king