Home / Learn Secure SQL / Python: pyodbc for SQL Server and Others

Python: pyodbc for SQL Server and Others

The common ODBC-based driver, used for SQL Server, and other ODBC sources.

In shortThe common ODBC-based driver, used for SQL Server, and other ODBC sources.

What's actually going on

pyodbc patterncursor.execute(sql,params)? placeholdersrows returned safely
pyodbc pattern

Definition

pyodbc connects to many databases through ODBC drivers (commonly SQL Server) and follows the same Python DB-API pattern as sqlite3 and psycopg2, with ? as the placeholder.

Why you need this

Its consistency with the other two Python drivers means the mental model from this whole part of the course applies directly.

How it works

Use ? placeholders and pass a tuple of values to execute(), exactly like sqlite3.

Where you'll see this

  • Web application code review
  • Backend development
  • Database administration
Python pyodbc
import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER=...;DATABASE=shop;UID=app;PWD=...')
cur = conn.cursor()
cur.execute('SELECT * FROM users WHERE email = ?', email)
row = cur.fetchone()

Test what you just read

1. pyodbc's placeholder character matches...