杰瑞科技汇

Python如何用pyodbc连接Mssql?

Of course! Here is a comprehensive guide to using Python with the pyodbc library to connect to a Microsoft SQL Server (MSSQL) database. This guide covers installation, connection strings, executing queries, handling data, and best practices.

Python如何用pyodbc连接Mssql?-图1
(图片来源网络,侵删)

Overview

  • Python: The programming language.
  • pyodbc: A robust and popular Python library that allows Python to connect to ODBC (Open Database Connectivity) drivers. It acts as a bridge between your Python code and the database.
  • Microsoft SQL Server: The relational database management system (RDBMS) you want to connect to.

Step 1: Prerequisites

Before you start, you need two things:

  1. Python: Make sure you have Python installed on your system. You can download it from python.org.
  2. SQL Server ODBC Driver: This is the most critical part. pyodbc needs a driver to communicate with SQL Server. You must install the correct driver for your system and SQL Server version.

How to Install the ODBC Driver:

  • For Windows: The easiest way is to install the ODBC Driver for SQL Server from Microsoft. Choose the appropriate version (e.g., "ODBC Driver 18 for SQL Server" is the latest and recommended).
  • For macOS: You can install it using Homebrew:
    brew update
    brew install microsoft/mssql/msodbcsql18

    You might also need the unixODBC development headers:

    brew install unixodbc
  • For Linux: Follow the official Microsoft documentation for your specific distribution (e.g., Ubuntu, Red Hat). It usually involves adding Microsoft's repositories and installing the package.

How to Verify Your Driver:

Python如何用pyodbc连接Mssql?-图2
(图片来源网络,侵删)

You can list the installed drivers on your system using a simple Python script:

import pyodbc
# List all installed ODBC drivers
drivers = pyodbc.drivers()
print("Available ODBC Drivers:")
for driver in drivers:
    print(f"- {driver}")
# Example output on Windows with the latest driver:
# Available ODBC Drivers:
# - ODBC Driver 18 for SQL Server
# - ODBC Driver 17 for SQL Server
# - SQL Server

Step 2: Installing pyodbc

Once you have the driver, installing pyodbc is straightforward using pip.

pip install pyodbc

Step 3: Creating a Connection String

The connection string is the most important part. It tells pyodbc which driver to use, where the server is, and how to authenticate. Here are the most common components:

Parameter Description Example
DRIVER The name of the ODBC driver you installed. {ODBC Driver 18 for SQL Server}
SERVER The name or IP address of your SQL Server instance. localhost, 168.1.100, SERVERNAME\INSTANCE
DATABASE The name of the database you want to connect to. master, MyDatabase
UID Your SQL Server username. myuser
PWD Your SQL Server password. mypassword
Trusted_Connection Use Windows Authentication instead of username/password. yes

Connection String Examples

Windows Authentication (Recommended for on-premises servers) This uses your current Windows login credentials to connect to SQL Server.

# Note the semicolons and the curly braces around the driver name
conn_str = (
    r'DRIVER={ODBC Driver 18 for SQL Server};'
    r'SERVER=your_server_name;'  # e.g., 'localhost' or 'SERVERNAME\SQLEXPRESS'
    r'DATABASE=your_database_name;'
    r'Trusted_Connection=yes;'
)

SQL Server Authentication (Username and Password) This is used when you have a specific SQL login.

conn_str = (
    r'DRIVER={ODBC Driver 18 for SQL Server};'
    r'SERVER=your_server_name;'
    r'DATABASE=your_database_name;'
    r'UID=your_username;'
    r'PWD=your_password;'
)

Connecting to Azure SQL Database The connection is very similar, but you often specify the Encrypt parameter.

conn_str = (
    r'DRIVER={ODBC Driver 18 for SQL Server};'
    r'SERVER=your_server_name.database.windows.net,1433;' # Note the port
    r'DATABASE=your_database_name;'
    r'UID=your_username;'
    r'PWD=your_password;'
    r'Encrypt=yes;'
    r'TrustServerCertificate=no;'
    r'Connection Timeout=30;'
)

Step 4: Connecting and Executing Queries

Here is a complete, commented example demonstrating how to connect, execute a query, and fetch data.

import pyodbc
# --- 1. Connection String ---
# Replace with your actual server and database details
# Using Windows Authentication
conn_str = (
    r'DRIVER={ODBC Driver 18 for SQL Server};'
    r'SERVER=DESKTOP-ABC\SQLEXPRESS;'  # Replace with your server name
    r'DATABASE=master;'
    r'Trusted_Connection=yes;'
)
# --- 2. Establish Connection ---
try:
    # The 'with' statement ensures the connection is automatically closed
    print("Attempting to connect to the database...")
    with pyodbc.connect(conn_str) as conn:
        print("Connection successful!")
        # --- 3. Create a Cursor ---
        # A cursor is an object that allows you to execute SQL commands
        cursor = conn.cursor()
        # --- 4. Execute a SELECT Query ---
        print("\nExecuting SELECT query...")
        cursor.execute("SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')")
        # --- 5. Fetch and Process Results ---
        # fetchone() retrieves a single row
        # row = cursor.fetchone()
        # if row:
        #     print(f"Fetched one row: {row}")
        # fetchall() retrieves all remaining rows
        databases = cursor.fetchall()
        print("\nFound the following databases:")
        for db in databases:
            print(f"- {db.name}")
        # --- 6. Execute an INSERT, UPDATE, or DELETE Query (DML) ---
        print("\nExecuting an INSERT statement...")
        # Use parameterized queries to prevent SQL injection!
        # The '?' is a placeholder.
        insert_query = "INSERT INTO Employees (FirstName, LastName, HireDate) VALUES (?, ?, ?)"
        employee_data = ('Jane', 'Doe', '2025-10-27')
        cursor.execute(insert_query, employee_data)
        # IMPORTANT: For DML operations (INSERT, UPDATE, DELETE), you must commit the transaction
        conn.commit()
        print(f"Successfully inserted 1 row into the 'Employees' table.")
except pyodbc.Error as e:
    print(f"Error connecting to SQL Server or executing query: {e}")
finally:
    # The 'with' statement handles closing the connection automatically
    # but it's good practice to be explicit if you don't use 'with'
    if 'conn' in locals() and conn:
        conn.close()
        print("\nDatabase connection closed.")

Best Practices & Important Concepts

Parameterized Queries (Preventing SQL Injection)

NEVER use Python string formatting ( or f-strings) to insert values directly into a SQL query. This is a massive security risk called SQL Injection.

BAD (Vulnerable to SQL Injection):

user_input = "admin' -- "
sql = f"SELECT * FROM Users WHERE Username = '{user_input}'"
cursor.execute(sql) # This could grant access to an attacker

GOOD (Safe and Recommended): Use the placeholder as shown in the INSERT example above. pyodbc will safely handle the data type conversion and escaping.

user_input = "admin' -- "
sql = "SELECT * FROM Users WHERE Username = ?"
cursor.execute(sql, user_input) # This is safe

Using with for Connection Management

The with statement is the recommended way to handle database connections. It guarantees that the connection will be closed automatically when the block is exited, even if an error occurs. This prevents resource leaks.

Fetching Data Efficiently

  • cursor.fetchone(): Use this if you expect only one result or are looping through results one by one. It's memory-efficient for large datasets.
  • cursor.fetchall(): Use this when you know the result set will be small and you want all rows at once. Warning: This can consume a lot of memory if the result set is very large.
  • cursor.fetchmany(size): A good compromise. It fetches a specific number of rows at a time.

Working with Pandas

For data analysis, you'll often want to get the results directly into a Pandas DataFrame. pyodbc makes this easy.

First, install pandas:

pip install pandas

Then, you can use pandas.read_sql_query():

import pandas as pd
import pyodbc
conn_str = (
    r'DRIVER={ODBC Driver 18 for SQL Server};'
    r'SERVER=DESKTOP-ABC\SQLEXPRESS;'
    r'DATABASE=master;'
    r'Trusted_Connection=yes;'
)
query = "SELECT TOP 10 * FROM sys.objects"
try:
    with pyodbc.connect(conn_str) as conn:
        # Read the query directly into a DataFrame
        df = pd.read_sql_query(query, conn)
        print("Successfully loaded data into a DataFrame:")
        print(df.head())
        # Now you can use all the power of pandas
        print(f"\nDataFrame shape: {df.shape}")
except pyodbc.Error as e:
    print(f"Error: {e}")
分享:
扫描分享到社交APP
上一篇
下一篇