Python Database Interaction: Common Patterns and a Practical User Registration Example
This article introduces ten common Python database interaction patterns—from basic CRUD operations and table creation to advanced transaction handling, batch inserts, and stored procedures—culminating in a complete user‑registration system example that demonstrates how to combine these techniques effectively.
Introduction
Database operations are an indispensable part of software development; mastering database interaction techniques is crucial for both beginners and experienced developers. This article details common Python‑database patterns, covering basic CRUD to advanced transaction handling.
1. Connecting to a Database
Python typically connects to databases using the sqlite3 module for SQLite or the psycopg2 library for PostgreSQL.
<code>import sqlite3
# Connect to SQLite database
connection = sqlite3.connect('example.db')
# Create a cursor object
cursor = connection.cursor()
</code>The connection object represents the database connection, while the cursor executes SQL commands and fetches results.
2. Creating a Table
After establishing a connection, a table must be created to store data.
<code># Create table
create_table_query = """
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);
"""
cursor.execute(create_table_query)
connection.commit() # Commit changes
</code>This creates a users table with id , name , and email columns.
3. Inserting Data
Data can be inserted into the table using a parameterized query to prevent SQL injection.
<code># Insert data
insert_query = "INSERT INTO users (name, email) VALUES (?, ?);"
user_data = ('John Doe', '[email protected]')
cursor.execute(insert_query, user_data)
connection.commit()
</code>4. Querying Data
Data retrieval is performed with a SELECT statement.
<code># Query data
select_query = "SELECT * FROM users WHERE name = ?;"
name_to_search = 'John Doe'
cursor.execute(select_query, (name_to_search,))
result = cursor.fetchone() # Get a single row
print(result)
</code>The fetchone() method returns the first matching row; fetchall() can be used for multiple rows.
5. Updating Data
Existing records can be modified with an UPDATE statement.
<code># Update data
update_query = "UPDATE users SET email = ? WHERE name = ?;"
new_email = '[email protected]'
cursor.execute(update_query, (new_email, name_to_search))
connection.commit()
</code>6. Deleting Data
Rows can be removed using a DELETE statement.
<code># Delete data
delete_query = "DELETE FROM users WHERE name = ?;"
cursor.execute(delete_query, (name_to_search,))
connection.commit()
</code>7. Using Transactions
Transactions ensure that a group of operations either all succeed or all fail.
<code>try:
connection.begin() # Start transaction
insert_query = "INSERT INTO users (name, email) VALUES (?, ?);"
cursor.execute(insert_query, ('Jane Doe', '[email protected]'))
update_query = "UPDATE users SET email = ? WHERE name = ?;"
cursor.execute(update_query, ('[email protected]', 'Jane Doe'))
connection.commit() # Commit transaction
except Exception as e:
connection.rollback() # Roll back transaction
print(f"Error occurred: {e}")
</code>8. Using a Context Manager
A context manager automatically closes the connection, even if an exception occurs.
<code>with sqlite3.connect('example.db') as connection:
cursor = connection.cursor()
# Perform database operations here
</code>9. Bulk Inserting Data
For large data sets, executemany inserts multiple rows efficiently.
<code># Bulk insert data
data_to_insert = [
('Alice Smith', '[email protected]'),
('Bob Johnson', '[email protected]'),
('Charlie Brown', '[email protected]')
]
insert_query = "INSERT INTO users (name, email) VALUES (?, ?);"
# Execute many
cursor.executemany(insert_query, data_to_insert)
connection.commit()
</code>10. Stored Procedures
Stored procedures are pre‑compiled SQL scripts that improve performance and reduce network overhead.
<code>-- Create stored procedure
CREATE PROCEDURE UpdateUserEmail(IN name_param TEXT, IN new_email TEXT)
BEGIN
UPDATE users SET email = new_email WHERE name = name_param;
END;
-- Call stored procedure
CALL UpdateUserEmail('Alice Smith', '[email protected]');
</code>In Python, the callproc method invokes a stored procedure.
<code># Call stored procedure
def call_update_user_email(cursor, name, new_email):
cursor.callproc('UpdateUserEmail', [name, new_email])
call_update_user_email(cursor, 'Alice Smith', '[email protected]')
connection.commit()
</code>Practical Example: User Registration System
This example combines all previous concepts to build a simple registration system supporting user creation, login verification, and profile updates.
<code>CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
</code> <code>def register_user(connection, username, password, email):
insert_query = "INSERT INTO users (username, password, email) VALUES (?, ?, ?);"
user_data = (username, password, email)
cursor = connection.cursor()
cursor.execute(insert_query, user_data)
connection.commit()
# Example registration
register_user(connection, 'testuser', 'password123', '[email protected]')
</code> <code>def login_user(connection, username, password):
select_query = "SELECT * FROM users WHERE username = ? AND password = ?;"
cursor = connection.cursor()
cursor.execute(select_query, (username, password))
result = cursor.fetchone()
return result is not None
# Example login
if login_user(connection, 'testuser', 'password123'):
print("Login successful!")
else:
print("Invalid credentials.")
</code> <code>def update_user_email(connection, username, new_email):
update_query = "UPDATE users SET email = ? WHERE username = ?;"
cursor = connection.cursor()
cursor.execute(update_query, (new_email, username))
connection.commit()
# Example update
update_user_email(connection, 'testuser', '[email protected]')
</code>Conclusion
The article covered ten common Python‑database interaction patterns, from basic CRUD to advanced transaction handling, batch inserts, and stored procedures, and demonstrated their combined use in a simple user registration system. Mastering these techniques enables more efficient and reliable database development.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.