Step by Step Tutorial on Programming SQLite Database Using Python for Beginners

by xanthium-enterprises in Circuits > Computers

76 Views, 1 Favorites, 0 Comments

Step by Step Tutorial on Programming SQLite Database Using Python for Beginners

python-sqlite-tutorial.png

In this Instructable, you will learn to program and store data to an SQLite 3 database using Python Programming Language. Here we plan to teach the user how to connect with a SQLite Database and read/write data into it for persistent storage.

We assume that the reader has basic understanding of the Python Programming Language and Simple SQL Database commands. I will try to explain the SQL commands as we go by the instructable.

This SQLite Tutorial is Cross-platform and is applicable

  1. on a Windows 10/11 System
  2. or any Linux System
  3. or macOS System

Here we will learn to create a connection to sqlite Database using Python Create tables, Add data, Update rows and finally delete data from the Db.

Supplies

SQLite370.svg.png

You will need the following things for going through this tutorial.


A Python Interpreter. You can download if from cPython .SQLite 3 Database comes with Python interpreter ,So no need to install it.


You can download DB Browser for SQLite to browse the created SQLite databases.


Source Codes can be downloaded from the GitHub Repo SQLite Tutorial using Python

What Is SQLite Database

mysql-database.jpg
sqlite3-database.jpg

SQLite is a lightweight relational database management system (RDBMS) used for storing data generated by your application software for persistent stoarge. Instead of storing data into to a text /binary file ,You can store the data into a SQLite Database which you can manipulate with SQL commands to get your desired data.

Lot of mainstream application like Firefox uses SQlite Database to store internal information like Browser History.


Traditional Database Systems

If you have ever used a Database like MySQL or PostgreSQL,

You will have a Server program (process) that runs as a Windows service (On windows Machine ) or as a daemon process in the case of Linux System called mysqld for MySQL.

The user will then connect to the server process using a command line called mysql using TCP/IP Protocol by giving a specific port number.

 mysqli python connect


No Server Needed

Unlike traditional client-server database engines (like MySQL or PostgreSQL), SQLite is server-less and runs in-process with the application.

It reads and writes directly to a single ordinary disk file as shown below.


 mysqli python connect

SQLite is an embedded database, meaning there is no need to install or configure a separate database server. It is compiled directly into the application and runs within the same process.

Being self-contained, the entire database,including the schema, tables, indexes, and data is stored in a single, cross-platform .sqlite or .db file.

It requires zero configuration, so applications can begin using it immediately without any setup or administrative overhead. SQLite follows the relational model, using standard SQL (with minor deviations) to define and manipulate data, supporting features such as tables, indexes, joins, transactions, and constraints. It is also ACID-compliant, fully supporting Atomicity, Consistency, Isolation, and Durability through transactional behavior, typically using a journal file or write-ahead logging (WAL) mode.

Furthermore, it is cross-platform, with a database file format that is portable between 32-bit and 64-bit systems and compatible across various operating systems.


Creating & Connecting to SQLite Using Python

create-emty-python-db.jpg
db-browser.jpg

First thing to do would be to import the sqlite3 module.It’s a built-in Python module, so you don't need to install anything extra.

import sqlite3

You can create a new database using the following line of code.

conn = sqlite3.connect('mydatabase.db') # create a connection object
conn.close()

Once you run this code ,

it will create a database file called mydatabase.db on your current directory. You can give any name or any file extension for your database.

Here conn variable is called as a connection object variable. A connection object in the context of SQLite represents an open connection between your application (in our case, your Python program) and the database. It is an instance of the sqlite3.Connection class in Python when you're using the sqlite3 module.

conn variable (connection object) establishes a communication link between your Python code and the SQLite database file.

Once you're done interacting with the database, you need to close the connection to release any resources using conn.close() method.

The above code will create a Sqlite database on your Disk but it will be completely empty.as you can see in the below image.

creating a connection to sqlite databse using python


You can either populate the database using an external Database editor like DB Browser for SQLite


creating a connection to sqlite database using python

or you can populate your SQLite db with tables programmatically using Python.


Basic SQLite Data Types for Storing Information

datatypes.jpg

SQLite provides five primary data types, also known as storage classes, that define how values are stored in a database. These data types ensure that the database can handle a wide range of information efficiently.

The five basic SQLite data types are:

  1. NULL –> Represents a missing or undefined value. It is used when no data is available for a particular field.
  2. INTEGER – >A signed whole number. Depending on the magnitude of the numbr, it can occupy 1, 2, 4, 6, or 8 bytes of storage.
  3. REAL –> A floating-point number, stored as an 8-byte IEEE 754 value. It is used for numeric values with decimal points.
  4. TEXT –> A text string, stored using Unicode encodings
  5. BLOB –> Stands for Binary Large Object. It is used to store binary data such as images, multimedia, or any file content in its raw form.


For most everyday applications, you'll primarily work with the following three types:

  1. TEXT – Used for storing strings, names, discriptions, and long pieces of textual information. Eg: 'Alice', 'New York', 'This is a sample note.'


  1. INTEGER – Ideal for storing whole numbers such as IDs, counts, or flags.Eg: 1, 25, 1003


  1. REAL – Used when you need to store numeric values with decimal points, such as prices, weights, or measurements. Eg: 3.14, 99.99, 0.0001

SQLite STRICT Mode

SQLite STRICT mode, introduced in version 3.37.0 is a feature that allows developers to create tables with much stricter type and integrity constraints than those enforced by default in SQLite.

Normally, SQLite uses dynamic typing, also known as manifest typing, which means that any value can be stored in any column, regardless of its declared type. STRICT tables override this behavior by requiring that values inserted into columns match the declared data type exactly.

For example, a column defined as TEXT will only accept actual text values, and an INTEGER column will reject floating-point or text values.


To create a STRICT table in SQLite, you use the STRICT keyword at the end of the CREATE TABLE statement.

A typical example might be:

CREATE TABLE users (id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
age INTEGER) STRICT; --make sure to add space between ) and STRICT;

In this table, any attempt to insert a non-text value into the username field, or a NULL value if it's declared as NOT NULL, would result in an error. This level of enforcement helps catch type-related bugs early and ensures data consistency.

However, it is important to note that STRICT tables are only available in SQLite version 3.37.0 and later, and not all features of SQLite are compatible with them, such as virtual tables.


Checking the version of SQLite Database


You can check the version of the sqlite database using the below code.

print("SQLite version:", sqlite3.sqlite_version)


sqlite3.sqlite_version will give you the version of the sqlite supported by the sqlite3 module in Python


Creating a Table Inside SQLite Using Python

create-strict-table-python-sqlite.jpg
image_139.png
code.jpg

In relational databases like SQLite, tables are the primary structures for storing data. Each table contains multiple columns (which define the data type and label for each piece of data) and multiple rows (which hold the actual data entries).

To store and organize data efficiently, we need to define and create these tables in advance.

Now that the database file exists (datalogger), we can create a table within it. This is done by executing a SQL CREATE TABLE statement using a cursor object obtained from the connection.

Here’s SQL to create a table with multiple columns which store REAL,TEXT and INTEGER datatypes

CREATE TABLE IF NOT EXISTS data_logger(
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, --Primary Key
Temp_Sensor1 REAL,
Temp_Sensor2 REAL,
Temp_Sensor3 REAL,
IP_Address TEXT,
TimeStamp INTEGER

) STRICT; -- Space before STRICT


Here we are using the SQLite STRICT option to male sure that declared datatypes match column names.

#Python code to create a table in sqlite using STRICT option

import sqlite3

create_table_sql_query = '''
CREATE TABLE IF NOT EXISTS data_logger(
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Temp_Sensor1 REAL,
Temp_Sensor2 REAL,
Temp_Sensor3 REAL,
IP_Address TEXT,
TimeStamp INTEGER

) STRICT;
'''
connection_object = sqlite3.connect('datalogger.sqlite')
cursor_object = connection_object.cursor()
cursor_object.execute(create_table_sql_query)
connection_object.commit()
connection_object.close()

At the end of the SQL statement for creating the table , the keyword STRICT is used, which activates STRICT mode for this table. This means that each column must strictly accept only values that conform to its declared data type. For example, attempting to insert a string into one of the REAL temperature columns would result in an error.

The script then connects to an SQLite database file by calling sqlite3.connect(). If the file does not exist, SQLite will create it.

A cursor object is created using connection_object.cursor(), which is used to execute SQL commands. A cursor object acts as an intermediary between your Python code and the SQLite database. It is used to execute SQL commands, retrieve query results, and manage the context of the database operation.

The execute() method of the cursor object is called with the SQL query to create the STRICT table.

After executing the command, connection_object.commit() is called to save the changes to the database,

and finally, the connection is closed using connection_object.close().

On Running this code using the Python Interpreter ,A SQLite database with STRICT table is created on your disk which you can view using DB Browser or DBeaver Program.


How to create a SQLite database with STRICT mode tables in Python

INSERTing Data Into SQLite Tables Using Python

image_143.png

CRUD, which stands for Create, Read, Update, and Delete, represents the four basic operations used in applications to interact with a database. These operations are the foundation of most web, desktop, and mobile applications that manage data.

  1. Create -> refers to inserting new data into the database, such as adding a new user or product.
  2. Read -> allows the application to retrieve and display existing data, like showing a list of blog posts or user profiles.
  3. Update -> to modify existing records, such as changing a user's email address or editing a product's price.
  4. Delete -> Removes unwanted or outdated data from the database, like deleting a post or removing an inactive user.

In real-world applications, CRUD operations appear everywhere. For Example, in an e-commerce website like amazon or flipkart, when a customer signs up, that action creates a new record in the users table. Viewing a list of products involves reading records from the database. When an administrator changes a product’s price, the update operation is used. If a product is discontinued, it can be removed using the delete operation.

In thi section we will INSERT data into our sqlite database using Python ,ie the Create part of our CRUD paradigm.

So we will use the Standard SQL to INSERT data into the table. Here is the Query for that.

INSERT INTO data_logger ( Temp_Sensor1,
Temp_Sensor2,
Temp_Sensor3,
IP_Address,
TimeStamp )
VALUES (25.30, 34.60, 24.18, '192.168.0.111', 1715087100);

Now you can run this query directly using cursor.execute() method and it will work.

But doing it exposes your database to SQL Injection attacks ,In which Rogue users could craft malicious SQL querys to execute inside your database causing data loss or corruption.

In our case when we are using Python to add data to the SQlite database ,we will be using some thing called parameterized queries.

Here Values are replaced with ? and the function adds the data after sanitizing it.

Now our parameterized SQL query will look like this.

insert_data_sql_query = '''
INSERT INTO data_logger (Temp_Sensor1,
Temp_Sensor2,
Temp_Sensor3,
IP_Address,
TimeStamp)
VALUES (?, ?, ?, ?, ?)
'''

Now you can use them in our code like this.

cursor_object.execute(insert_data_sql_query, (temp1, temp2, temp3, ip_address, timestamp))

Here is the full code for it .

# Sample data
temp1 = 25.3
temp2 = 34.6
temp3 = 24.8
ip_address = '192.168.0.101'
timestamp = int(time.time()) # current Unix time
insert_data_sql_query = '''
INSERT INTO data_logger (Temp_Sensor1,
Temp_Sensor2,
Temp_Sensor3,
IP_Address,
TimeStamp)
VALUES (?, ?, ?, ?, ?)
'''
# Connect and insert data
with sqlite3.connect('datalogger.sqlite') as connection_object:
cursor_object = connection_object.cursor()
cursor_object.execute(insert_data_sql_query, (temp1, temp2, temp3, ip_address, timestamp))




Reading Data From SQLite Database Rows Using Python

reading-from-sqlite.png

Here we will read a row from the SQLite database using Python language after executing a SQL query.

To read data from a sqlite table,Python's sqlite3 module provides three methods.They are

  1. The fetchone() method is used to retrieve a single row from the result set of a SELECT query and returns it as a tuple. If there are no more rows to fetch, it returns None. This method is efficient for applications that work with large tables and only need one row at a time, helping to conserve memory by not loading the entire result set.
  2. fetchall() retrieves all rows returned by the query and stores them in a list of tuples. While this is straightforward and convenient, it can lead to high memory usage if the result set is large, as it loads everything into memory at once
  3. fetchmany(n) is used to fetch the next n rows from the result set. It returns a list of tuples, where each tuple corresponds to one row, and if fewer than n rows remain, it returns only the remaining rows. This method is particularly helpful when working with large datasets, as it enables processing data in smaller, more manageable chunks rather than retrieving everything at once.


Here we will be running the following SQL Query

SELECT * FROM data_logger

Since our database is quite small, we can easily use the fetchall() method.

Here is the Code for reading a row from sqlite .

with sqlite3.connect('datalogger.sqlite') as connection_object:
cursor_object = connection_object.cursor()
cursor_object.execute('SELECT * FROM data_logger') #Execute a SELECT query
rows = cursor_object.fetchall() #Fetch all rows
# Print each row
for row in rows:
print(row)



UPDATE the Data in the SQLite Databse Using Python

update-sqlite-databse-python.jpg
image_147.png

Updating a row in SQLite using Python involves modifying existing data in a specific table based on a condition using the UPDATE SQL statement.

Usually the query will look like.

UPDATE table_name
SET field_name1 = variable, field_name2 = variable
WHERE Condition

So in our case it would look like

update_data_sql_query = ''' UPDATE data_logger SET Temp_Sensor1 = ?, Temp_Sensor2 = ?, Temp_Sensor3 = ? WHERE TimeStamp = ? '''

Now the partial code for updating the sqlite database using Python will look like this.

# New data that will be used to update.
new_temp1 = 2900.5
new_temp2 = 3500.0
new_temp3 = 2800.7
timestamp_to_update = 1746599012 # time stamp of the first row

# Connect to database
with sqlite3.connect("datalogger.sqlite") as connnection_object:
cursor_object = connnection_object.cursor()
cursor_object.execute(update_data_sql_query, (new_temp1, new_temp2, new_temp3, timestamp_to_update))
connnection_object.commit()

In the above code We will adding the new data based on the timestamp.

Before updating the table ,row will look like this.

How to update a SQLite database with STRICT mode tables in Python


Now you can see the temperature values has been updated inside the table( below)


How to update a SQLite database with STRICT mode tables in Python

DELETE the Data in the SQLite Table Using Python

To delete a row in SQLite using Python, you execute the DELETE FROM SQL command in combination with a WHERE clause that identifies the specific row or rows you want to remove from the table.

The DELETE FROM statement on its own will remove all rows from the specified table, so the WHERE clause is crucial when you intend to delete only certain entries rather than clearing the entire table.

The WHERE condition typically targets a unique value, such as an ID or a specific field, to ensure only the intended record is deleted. Without this condition, there is a risk of unintentionally erasing large amounts of data.

Here we will delete the row whose time stamp matches 1746599012..

Here is the partial code for deleting a row from the sqlite database using Python.

delete_sql_query = "DELETE FROM data_logger WHERE TimeStamp = ?"

with sqlite3.connect("datalogger.sqlite") as connnection_object:
cursor_object = connnection_object.cursor()
cursor_object.execute(delete_sql_query, (timestamp_to_delete,))
connnection_object.commit()