杰瑞科技汇

Python如何从数据库高效读取数据?

Of course! Connecting to a database from Python is a fundamental skill for many applications, from web development to data analysis. The process involves using a database driver (or connector) specific to the database you're using.

Python如何从数据库高效读取数据?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering the most common databases, from basic queries to more advanced topics like connection pooling and Object-Relational Mapping (ORM).

The Core Concept: The Workflow

No matter which database you use, the basic steps are the same:

  1. Import the Driver: Install and import the appropriate Python library for your database.
  2. Establish a Connection: Create a connection object using your database credentials (host, user, password, database name).
  3. Create a Cursor: A cursor is an object that allows you to execute SQL commands and fetch results.
  4. Execute a Query: Run your SQL statement (e.g., SELECT, INSERT, UPDATE).
  5. Fetch Results (if applicable): For SELECT queries, retrieve the data from the cursor.
  6. Commit Changes (if applicable): For INSERT, UPDATE, or DELETE queries, commit the transaction to save the changes.
  7. Close Everything: Close the cursor and then the connection to free up resources.

SQLite (The Simplest Case)

SQLite is a serverless, file-based database. It's perfect for beginners, small applications, or local testing because it doesn't require a separate server. The driver is built into Python's standard library.

Example:

Python如何从数据库高效读取数据?-图2
(图片来源网络,侵删)
import sqlite3
# --- Step 1 & 2: Connect to the database (it will be created if it doesn't exist) ---
# The database file will be 'my_database.db'
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()
# --- Step 3 & 4: Create a table and insert data ---
cursor.execute('''
    CREATE TABLE IF NOT EXISTS users (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        email TEXT NOT NULL UNIQUE
    )
''')
# Insert a new user
cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ('Alice', 'alice@example.com'))
# --- Step 5: Fetch results ---
# Let's see what's in the table now
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
print("--- All Users ---")
for row in rows:
    print(row)
# --- Step 6: Commit the changes ---
conn.commit()
# --- Step 7: Close the connection ---
cursor.close()
conn.close()

Key Point: For security, always use parameterized queries ( placeholders) to prevent SQL injection attacks. Never format user input directly into the query string.


PostgreSQL (The Robust Choice)

PostgreSQL is a powerful, open-source relational database. The most popular driver is psycopg2.

First, install the driver:

pip install psycopg2-binary

Example:

Python如何从数据库高效读取数据?-图3
(图片来源网络,侵删)
import psycopg2
# --- Step 1 & 2: Connect to the database ---
# Replace with your actual database credentials
conn = psycopg2.connect(
    dbname="your_db",
    user="your_user",
    password="your_password",
    host="localhost",
    port="5432"
)
cursor = conn.cursor()
# --- Step 3 & 4: Execute a query ---
# Note: Psycopg2 uses %s as the placeholder, not ?
cursor.execute("SELECT version();")
db_version = cursor.fetchone() # fetchone() gets the first row
print(f"PostgreSQL version: {db_version[0]}")
# --- Step 5: Fetch multiple rows ---
cursor.execute("SELECT id, name FROM users;")
users = cursor.fetchall()
print("\n--- Users ---")
for user in users:
    print(f"ID: {user[0]}, Name: {user[1]}")
# --- Step 6: Commit changes ---
conn.commit()
# --- Step 7: Close everything ---
cursor.close()
conn.close()

MySQL / MariaDB

MySQL and its fork MariaDB are extremely popular web databases. The standard driver is mysql-connector-python.

First, install the driver:

pip install mysql-connector-python

Example:

import mysql.connector
from mysql.connector import Error
try:
    # --- Step 1 & 2: Connect to the database ---
    conn = mysql.connector.connect(
        host='localhost',
        database='your_db',
        user='your_user',
        password='your_password'
    )
    if conn.is_connected():
        cursor = conn.cursor()
        # --- Step 3 & 4: Execute a query ---
        cursor.execute("SHOW TABLES;")
        tables = cursor.fetchall()
        print("--- Tables in Database ---")
        for table in tables:
            print(table[0])
        # --- Step 5: Fetch data ---
        cursor.execute("SELECT * FROM users;")
        users = cursor.fetchall()
        print("\n--- Users ---")
        for user in users:
            print(user)
except Error as e:
    print(f"Error while connecting to MySQL: {e}")
finally:
    # --- Step 7: Close everything ---
    if 'conn' in locals() and conn.is_connected():
        cursor.close()
        conn.close()
        print("\nMySQL connection is closed.")

SQL Server

Microsoft's SQL Server is common in enterprise environments. The standard driver is pyodbc.

First, install the driver:

pip install pyodbc

You will also need to install the official Microsoft ODBC driver for SQL Server on your system.

Example:

import pyodbc
# --- Step 1 & 2: Connect to the database ---
# Connection string format can vary
conn_str = (
    'DRIVER={ODBC Driver 17 for SQL Server};'
    'SERVER=your_server_name;'
    'DATABASE=your_db;'
    'UID=your_user;'
    'PWD=your_password'
)
conn = pyodbc.connect(conn_str)
cursor = conn.cursor()
# --- Step 3 & 4: Execute a query ---
cursor.execute("SELECT TOP 3 name FROM sys.tables")
tables = cursor.fetchall()
print("--- Tables ---")
for table in tables:
    print(table.name)
# --- Step 5: Fetch data ---
cursor.execute("SELECT * FROM users")
users = cursor.fetchall()
print("\n--- Users ---")
for user in users:
    # Columns are accessed by index
    print(f"ID: {user[0]}, Name: {user[1]}")
# --- Step 6: Commit changes ---
conn.commit()
# --- Step 7: Close everything ---
cursor.close()
conn.close()

Best Practices & Advanced Topics

Using with Statements for Automatic Resource Management

Manually closing connections and cursors is error-prone. A with statement ensures they are closed automatically, even if errors occur.

import sqlite3
def get_users():
    # The 'with' statement handles closing the connection and cursor
    with sqlite3.connect('my_database.db') as conn:
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM users")
        return cursor.fetchall()
# Call the function
users = get_users()
print(users)
# No need to call conn.close() here!

Connection Pooling

Opening and closing a database connection for every request is slow. A connection pool maintains a set of open connections that can be reused, significantly improving performance.

Many libraries support this out of the box. For psycopg2, you can use psycopg2.pool.

import psycopg2
from psycopg2 import pool
# Create a connection pool
# minconn: minimum number of connections
# maxconn: maximum number of connections
connection_pool = psycopg2.pool.SimpleConnectionPool(
    minconn=1,
    maxconn=5,
    dbname="your_db",
    user="your_user",
    password="your_password",
    host="localhost"
)
# Get a connection from the pool
conn = connection_pool.getconn()
try:
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users;")
    users = cursor.fetchall()
    print(users)
finally:
    # Return the connection to the pool
    connection_pool.putconn(conn)
# Close the pool when your application shuts down
connection_pool.closeall()

Object-Relational Mapping (ORM)

An ORM allows you to interact with your database using Python objects and classes instead of writing raw SQL. This makes your code more Pythonic, easier to read, and less prone to SQL injection.

SQLAlchemy is the most popular ORM for Python.

First, install SQLAlchemy:

pip install SQLAlchemy

Example with SQLAlchemy:

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# 1. Setup the engine and session
# 'echo=True' will print the generated SQL
engine = create_engine('sqlite:///my_orm_database.db', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
# 2. Define the mapping (Python class -> SQL table)
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
分享:
扫描分享到社交APP
上一篇
下一篇