To connect Python to Microsoft SQL Server, you can use the pyodbc library, which provides a Python DB API interface for connecting to various database systems, including SQL Server. Here’s how you can establish a connection and perform basic database operations:
- Install pyodbc: If you haven’t already, you need to install the
pyodbclibrary. You can usepipfor this:
pip install pyodbc- Import the Library: Import the
pyodbclibrary at the beginning of your Python script:
import pyodbc- Establish a Connection: Create a connection to the SQL Server database using the
pyodbc.connect()function, providing the necessary connection parameters (such as server, database, user, and password):
conn = pyodbc.connect(
'Driver={SQL Server};'
'Server=server_name;'
'Database=database_name;'
'UID=username;'
'PWD=password;'
)Replace server_name, database_name, username, and password with your actual connection details.
- Create a Cursor: Create a cursor object to interact with the database. The cursor is used to execute SQL queries and fetch results.
cursor = conn.cursor()- Execute SQL Queries: You can now execute SQL queries using the cursor’s
execute()method. For example, to fetch data from a table:
cursor.execute("SELECT * FROM employees")
data = cursor.fetchall()
for row in data:
print(row)- Commit and Close: After executing your queries, make sure to commit the changes (if any) and close the cursor and database connection:
conn.commit()
cursor.close()
conn.close()Here’s a complete example that demonstrates connecting Python to SQL Server using pyodbc:
import pyodbc
# Establish a connection
conn = pyodbc.connect(
'Driver={SQL Server};'
'Server=server_name;'
'Database=database_name;'
'UID=username;'
'PWD=password;'
)
# Create a cursor
cursor = conn.cursor()
# Execute an SQL query
cursor.execute("SELECT * FROM employees")
data = cursor.fetchall()
for row in data:
print(row)
# Commit and close
conn.commit()
cursor.close()
conn.close()Replace server_name, database_name, username, password, and "employees" with your actual connection details and table name.
pyodbc provides a flexible and efficient way to work with SQL Server databases in Python. It supports a variety of connection options and provides the ability to execute queries, fetch results, and handle database operations.