↓ Skip to main content
Python

Python & DB

·2 mins

SQLite3 is a lightweight and easy-to-use database engine that is included with Python. It’s perfect for smaller-scale applications, prototyping, or standalone systems. Python’s sqlite3 module allows seamless integration with SQLite databases.

Here’s a comprehensive guide on using Python 3 with SQLite3.

Step 1: Importing the sqlite3 Module
#

Start by importing the sqlite3 module in your Python script:

import sqlite3

Step 2: Connecting to a Database
#

To use an SQLite database, establish a connection using sqlite3.connect():

conn = sqlite3.connect('example.db')

Step 3: Creating a Cursor Object
#

Create a cursor object to interact with the database:

cursor = conn.cursor()

Step 4: Creating a Table
#

Use the cursor to execute SQL queries. For instance, to create a ‘users’ table:

cursor.execute('''CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL )''')

Step 5: Inserting Data
#

Insert data into the table using the execute() method:

cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ('John', 'John@example.com')) cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ('Doe', 'Doe@example.com'))

Step 6: Committing Changes
#

Save the changes made to the database:

conn.commit()

Step 7: Retrieving Data
#

Retrieve data from the table using SQL queries:

cursor.execute("SELECT * FROM users") rows = cursor.fetchall() for row in rows: print(row)

Step 8: Closing the Connection
#

Close the connection to the database when finished:

conn.close()

Conclusion
#

Python’s integration with SQLite3 makes it effortless to create, manipulate, and manage databases within your Python scripts. SQLite3’s simplicity and Python’s versatility make it an ideal choice for small to medium-scale projects or standalone systems where a lightweight, file-based database is sufficient.

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Python Virtual Enviroments

·3 mins
Recreated in October 2026 from the post; the original image was lost Using a virtual environment like venv is a best practice when working with Python-based projects like Ansible, Molecule, and Playbooks. Continuing from the blog, Virtual environments are isolated Python environments that allow you to manage dependencies separately for each project, providing a controlled and reproducible environment for development, testing, and production.

VENV: Ansible

·2 mins
Recreated in October 2026 from the post; the original image was lost Ansible and Molecule are popular tools for configuration management and testing in the world of DevOps. To avoid conflicts between different projects and their dependencies, it is recommended to use a virtual environment for each project. In this blog post, we’ll show you how to set up and use a virtual environment using Python’s built-in venv module on macOS and CentOS 7.

Python: Sample Project

·2 mins
Python is a high-level programming language that is widely used for web development, machine learning, scientific computing and more. In this blog post, we will be identify (and build) how to develop a simple python project with unit testing, publishing it finally as a binary and distribute it as source.