Python sqlite3: Working with multiple databases

Updated: February 6, 2024 By: Guest Contributor Post a comment

Overview

SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. It is the most used database engine in the world. Python’s sqlite3 module provides a straightforward interface to interact with SQLite databases. In this tutorial, we will explore how to work with multiple SQLite databases using Python’s sqlite3 module. We will start with basic operations and gradually move to more advanced techniques.

Setting Up Your Environment

Before diving into the code, ensure that you have Python installed on your system. This tutorial assumes you have basic knowledge of Python and SQL. No additional libraries are required since sqlite3 is included in the Python standard library.

Connecting to Multiple Databases

The first step in working with multiple SQLite databases is to establish connections to those databases. Each database requires its individual connection object. Here’s how you can connect to two databases:

import sqlite3

# Connect to the first database
db1_conn = sqlite3.connect('database1.db')

# Connect to the second database
db2_conn = sqlite3.connect('database2.db')

This will create two database files if they don’t already exist and establish connections to them. You can also work with in-memory databases by passing “:memory:” instead of a file name.

Executing SQLite Commands Across Databases

After establishing connections, the next step is to execute SQLite commands. You will need a cursor object for each database to execute SQL commands. Here’s how to create cursor objects and execute a simple command:

db1_cursor = db1_conn.cursor()
db2_cursor = db2_conn.cursor()

# Creating a table in the first database
db1_cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")

# Creating a similar table in the second database
db2_cursor.execute("CREATE TABLE IF NOT EXISTS clients (client_id INTEGER PRIMARY KEY, client_name TEXT, client_age INTEGER)")

Note that despite working with different databases, the process of executing commands is the same.

Transferring Data Between Databases

One common task when working with multiple databases is transferring data from one database to another. This can be executed with a combination of SELECT and INSERT commands. Here’s an example:

# Inserting data into the first database
db1_cursor.execute("INSERT INTO users (name, age) VALUES ('Alice', 30)")
db1_conn.commit()

# Transferring data from the first to the second database
db1_cursor.execute("SELECT * FROM users")
users = db1_cursor.fetchall()

for user in users:
    query = f"INSERT INTO clients (client_id, client_name, client_age) VALUES (?, ?, ?)"
    db2_cursor.execute(query, (user[0], user[1], user[2]))
db2_conn.commit()

This code snippet demonstrates how to insert a record into the first database and then transfer it to the second database. Don’t forget to commit your transactions using the commit() method.

Advanced Operations: Joining Data Across Databases

SQLite does not support executing a JOIN operation directly across databases. However, you can accomplish this task by fetching data from each database and combining it manually in Python. Here is an example:

# Fetching data from the first database
db1_cursor.execute("SELECT * FROM users WHERE age > 25")
users_over_25 = db1_cursor.fetchall()

# Fetching data from the second database
db2_cursor.execute("SELECT * FROM clients WHERE client_age > 25")
clients_over_25 = db2_cursor.fetchall()

# Manually joining data based on a condition (e.g., age)
joined_data = []
for user in users_over_25:
    for client in clients_over_25:
        if user[2] == client[2]:  # Matching based on age
            joined_data.append((user, client))

This segment illustrates a manual join operation based on a matching condition (age in this instance). This method is not as efficient as SQL joins, but it is a practical workaround for joining data across databases in SQLite.

Conclusion

Working with multiple SQLite databases in Python provides a flexible way to manage and manipulate data across different data stores. By leveraging Python’s sqlite3 module, developers can efficiently execute basic to advanced database operations. This tutorial covered how to connect to multiple databases, execute commands, transfer data, and manually join data from different databases. It illustrates the versatility and power of combining Python with SQLite for database management.