Article image Database Interaction with Python

26. Database Interaction with Python

Page 61 | Listen in audio

26. Database Interaction with Python

In the realm of automating everyday tasks, one of the most powerful applications of Python is its ability to interact with databases. Databases are integral to modern applications, serving as the backbone for storing, retrieving, and managing data. Whether you're developing a web application, a data analysis tool, or any software that requires data persistence, understanding how to interact with databases using Python is essential.

Understanding Databases

A database is an organized collection of data, generally stored and accessed electronically. Databases can be categorized into several types, the most common being relational databases like MySQL, PostgreSQL, and SQLite, and non-relational databases like MongoDB. Relational databases use structured query language (SQL) for defining and manipulating data, while non-relational databases (often referred to as NoSQL databases) can store unstructured data and often use JSON-like documents.

Why Use Python for Database Interaction?

Python is a versatile language that offers a wide array of libraries and frameworks for database interaction. Its simplicity and readability make it an ideal choice for both beginners and experienced developers. Python's extensive library ecosystem includes tools for connecting to almost any database, performing complex queries, and even managing database schema migrations.

Setting Up Your Environment

Before diving into database interaction, you need to set up your Python environment with the necessary libraries. For relational databases, the most commonly used libraries are:

  • SQLite3: Comes built-in with Python and is perfect for small to medium-sized applications.
  • PyMySQL: A pure-Python MySQL client.
  • psycopg2: A popular PostgreSQL adapter for Python.

For non-relational databases like MongoDB, you can use:

  • PyMongo: The official MongoDB driver for Python.

To install these packages, you can use pip, Python's package installer. For example:

pip install pymysql psycopg2 pymongo

Connecting to a Database

Let's explore how to connect to different types of databases using Python.

Connecting to SQLite

SQLite is a lightweight, disk-based database that doesn’t require a separate server process. It is ideal for testing and small applications. Since it's built into Python, you can get started without any additional installations:

import sqlite3

# Connect to an SQLite database (or create it if it doesn't exist)
connection = sqlite3.connect('example.db')

# Create a cursor object using the cursor() method
cursor = connection.cursor()

# Execute a simple query
cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")

# Commit the changes
connection.commit()

# Close the connection
connection.close()

Connecting to MySQL

To connect to a MySQL database, you’ll need the PyMySQL library. Here’s how you can establish a connection:

import pymysql

# Establish a database connection
connection = pymysql.connect(
    host='localhost',
    user='yourusername',
    password='yourpassword',
    database='yourdatabase'
)

# Create a cursor object
cursor = connection.cursor()

# Execute a query
cursor.execute("SHOW TABLES")

# Fetch and print the result
tables = cursor.fetchall()
for table in tables:
    print(table)

# Close the connection
connection.close()

Connecting to PostgreSQL

For PostgreSQL, the psycopg2 library is commonly used. Here’s how you can connect to a PostgreSQL database:

import psycopg2

# Establish a database connection
connection = psycopg2.connect(
    dbname='yourdatabase',
    user='yourusername',
    password='yourpassword',
    host='localhost'
)

# Create a cursor object
cursor = connection.cursor()

# Execute a query
cursor.execute("SELECT version()")

# Fetch and print the result
db_version = cursor.fetchone()
print(db_version)

# Close the connection
connection.close()

Connecting to MongoDB

MongoDB is a popular NoSQL database, and PyMongo is the library used for connecting to it. Here’s a basic example:

from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')

# Access a database
db = client['yourdatabase']

# Access a collection
collection = db['yourcollection']

# Insert a document
collection.insert_one({"name": "Alice", "age": 30})

# Fetch and print a document
document = collection.find_one({"name": "Alice"})
print(document)

Performing CRUD Operations

CRUD stands for Create, Read, Update, and Delete. These are the four basic operations you can perform on any database. Let's explore how to perform these operations using Python.

SQLite CRUD Operations

Here’s how you can perform CRUD operations with SQLite:

import sqlite3

# Connect to an SQLite database
connection = sqlite3.connect('example.db')
cursor = connection.cursor()

# Create
cursor.execute("INSERT INTO users (name, age) VALUES ('Bob', 25)")
connection.commit()

# Read
cursor.execute("SELECT * FROM users")
users = cursor.fetchall()
for user in users:
    print(user)

# Update
cursor.execute("UPDATE users SET age = 26 WHERE name = 'Bob'")
connection.commit()

# Delete
cursor.execute("DELETE FROM users WHERE name = 'Bob'")
connection.commit()

# Close the connection
connection.close()

MySQL CRUD Operations

Performing CRUD operations in MySQL is similar to SQLite, with some differences in syntax:

import pymysql

# Connect to the database
connection = pymysql.connect(
    host='localhost',
    user='yourusername',
    password='yourpassword',
    database='yourdatabase'
)
cursor = connection.cursor()

# Create
cursor.execute("INSERT INTO users (name, age) VALUES ('Alice', 30)")
connection.commit()

# Read
cursor.execute("SELECT * FROM users")
users = cursor.fetchall()
for user in users:
    print(user)

# Update
cursor.execute("UPDATE users SET age = 31 WHERE name = 'Alice'")
connection.commit()

# Delete
cursor.execute("DELETE FROM users WHERE name = 'Alice'")
connection.commit()

# Close the connection
connection.close()

PostgreSQL CRUD Operations

For PostgreSQL, CRUD operations are performed similarly, with some differences in the connection and execution methods:

import psycopg2

# Connect to the database
connection = psycopg2.connect(
    dbname='yourdatabase',
    user='yourusername',
    password='yourpassword',
    host='localhost'
)
cursor = connection.cursor()

# Create
cursor.execute("INSERT INTO users (name, age) VALUES (%s, %s)", ('Charlie', 28))
connection.commit()

# Read
cursor.execute("SELECT * FROM users")
users = cursor.fetchall()
for user in users:
    print(user)

# Update
cursor.execute("UPDATE users SET age = %s WHERE name = %s", (29, 'Charlie'))
connection.commit()

# Delete
cursor.execute("DELETE FROM users WHERE name = %s", ('Charlie',))
connection.commit()

# Close the connection
connection.close()

MongoDB CRUD Operations

With MongoDB, the operations are slightly different due to its document-based nature:

from pymongo import MongoClient

# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['yourdatabase']
collection = db['yourcollection']

# Create
collection.insert_one({"name": "David", "age": 35})

# Read
documents = collection.find({"name": "David"})
for doc in documents:
    print(doc)

# Update
collection.update_one({"name": "David"}, {"$set": {"age": 36}})

# Delete
collection.delete_one({"name": "David"})

Using ORM with Python

Object-Relational Mapping (ORM) is a technique that allows you to interact with your database using objects in your programming language. This abstraction layer simplifies database interactions by allowing you to work with Python objects instead of writing SQL queries directly. SQLAlchemy is a popular ORM library in Python.

Setting Up SQLAlchemy

To use SQLAlchemy, you need to install it using pip:

pip install sqlalchemy

Here’s how you can use SQLAlchemy to interact with a database:

from sqlalchemy import create_engine, Column, Integer, String, Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# Define the database URL
DATABASE_URL = 'sqlite:///example.db'

# Create a new SQLAlchemy engine
engine = create_engine(DATABASE_URL)

# Create a base class for declarative class definitions
Base = declarative_base()

# Define a User class
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String(50))
    age = Column(Integer)

# Create the users table
Base.metadata.create_all(engine)

# Create a new session
Session = sessionmaker(bind=engine)
session = Session()

# Create a new user
new_user = User(name='Eve', age=22)
session.add(new_user)
session.commit()

# Query the database
for user in session.query(User).filter_by(name='Eve'):
    print(user.name, user.age)

# Update a user
user_to_update = session.query(User).filter_by(name='Eve').first()
user_to_update.age = 23
session.commit()

# Delete a user
session.delete(user_to_update)
session.commit()

Conclusion

Interacting with databases using Python is a fundamental skill for any developer looking to automate tasks or build applications that require data persistence. Whether you're working with relational databases like SQLite, MySQL, and PostgreSQL, or non-relational databases like MongoDB, Python provides robust libraries and tools to simplify these interactions. By understanding both direct database connections and using ORMs like SQLAlchemy, you can choose the best approach for your project’s needs, ensuring efficient and effective data management.

As you continue to explore Python's capabilities, consider the importance of database optimization, security practices, and scalability to ensure your applications perform well and remain secure as they grow.

Now answer the exercise about the content:

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Generating Reports with Matplotlib

Next page of the Free Ebook:

62Generating Reports with Matplotlib

10 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text