Python SQLite Tutorial

In this Python SQLite article we want to learn about Python SQLite Tutorial, Python is a powerful programming language and it is commonly used for different applications, including web development, data analysis and scientific computing. in this tutorial we want to talk about SQLite database programming with Python.

 

What is SQLite ?

SQLite is popular database management system and it is mostly used by Python developers. SQLite is serverless, self contained, zero configuration and transactional database engine that is embedded in most modern operating systems. this means that it is lightweight and easy to use, and it is a great choice for small to medium sized applications.

 

 

In this Python SQLite tutorial, we want to cover the basics of working with SQLite databases in Python. we will go through the steps of creating and connecting to a database, inserting and retrieving data and updating and deleting data.

 

 

For working with SQLite database in Python, we need to first create a database and connect to it. for creating a new database, we simply need to import the SQLite3 module and execute this code.

In the above code, we are going to create a new database called geekscoders.db and after that we use connect() method of the sqlite3 module. if the database already exists, connect() method will connect to it. if it does not exist, it will create a new one.

 

 

We have our database, now we need to create our table in the database, we can use this code, this code will create employees table in our database, but right now we don’t have any data in our database.

 

 

You can see that we have our SQLite table.

Python SQLite Tutorial
Python SQLite Tutorial

 

 

 

After that we have created and connected to a database, we can start inserting data into it. for inserting data into SQLite database, we use execute() method of the database connection object, we want to add two data in the table.

 

 

This will be the result

Python SQLite
Python SQLite

 

 

For retrieving data from SQLite database, we can use execute() method of the database connection object with SELECT statement, in this code we are selecting all records from a table called employees and print each row to the console. execute() method returns a cursor object that we can iterate over to retrieve the rows.

 

 

This will be the result

SQLite Tutorial in Python
SQLite Tutorial in Python

 

 

For updating data in SQLite database, we use execute() method of the database connection object with an UPDATE statement, this code will update the name.

 

 

This will be the result

Python SQLite
Python SQLite

 

 

 

To delete data from SQLite database, we use execute() method of the database connection object with DELETE statement. 

 

 

Learn More

Leave a Comment