Programming Search⌘K

Featurecore

Database libraries

Also called DB-API, PEP 249, sqlite3, SQLAlchemy, Psycopg, paramstyle, SQL injection, ORM.

PEP 249, the DB-API, gives database modules one shape: connect, cursor, execute, fetch, commit. sqlite3 is in the standard library. Psycopg and SQLAlchemy are on PyPI. Bind values with placeholders.

Language
Python

Synopsis

import contextlib
import sqlite3

with contextlib.closing(sqlite3.connect(':memory:')) as con:
    con.execute('CREATE TABLE lang(name TEXT, year INTEGER)')
    with con:
        con.executemany('INSERT INTO lang VALUES (?, ?)',
                        [('Python', 1991), ('Go', 2009), ('C', 1972)])
    cur = con.execute('SELECT name FROM lang WHERE year > ? ORDER BY year', (1980,))
    print(cur.fetchall())
Output
[('Python',), ('Go',)]

A database module of Python follows the DB-API of PEP 249. The program connects, executes SQL with placeholders such as ?, fetches the rows and commits. sqlite3 is in the standard library. Psycopg is the adapter for PostgreSQL, and SQLAlchemy is a toolkit and an ORM that works on top of the adapters.

Semantics

r1. PEP 249 "has been defined to encourage similarity between the Python modules that are used to access databases."1 The aim is "code that is generally more portable across databases".1

r2. A module gives a connection, and the connection gives cursors. A cursor is "used to manage the context of a fetch operation."1 Example: sqlite.

r3. The global paramstyle is a "String constant stating the type of parameter marker formatting expected by the interface."1 The values are qmark (WHERE name=?), numeric (WHERE name=:1), named (WHERE name=:name), format (WHERE name=%s) and pyformat (WHERE name=%(name)s).1 Example: paramstyle.

r4. "SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process".2 The module sqlite3 "provides an SQL interface compliant with the DB-API 2.0 specification described by PEP 249".2

r5. "beware of using Python’s string operations to assemble queries, as they are vulnerable to SQL injection attacks."2 Bind the values with placeholders. Example: injection.

r6. "A Connection object can be used as a context manager that automatically commits or rolls back open transactions when leaving the body of the context manager."2 "The context manager neither implicitly opens a new transaction nor closes the connection."2 Example: rollback.

r7. "Psycopg 3 is a newly designed PostgreSQL database adapter for the Python programming language."3 It "presents a familiar interface for everyone who has used Psycopg 2 or any other DB-API 2.0 database adapter".3

r8. "The SQLAlchemy SQL Toolkit and Object Relational Mapper is a comprehensive set of tools for working with databases and Python."4 "SQLAlchemy is designed to operate with a DBAPI implementation built for a particular database".4

Examples

sqlite3 uses the qmark style, and it also accepts the named style with a dict (rule r3):

import contextlib
import sqlite3

print(sqlite3.paramstyle)
with contextlib.closing(sqlite3.connect(':memory:')) as con:
    row = con.execute('SELECT :a + :b', {'a': 2, 'b': 3}).fetchone()
    print(row)
Output
qmark
(5,)

A query built with string formatting runs the text of the attacker. A placeholder sends the same text as a value (rule r5):

import contextlib
import sqlite3

with contextlib.closing(sqlite3.connect(':memory:')) as con:
    con.execute('CREATE TABLE users(name TEXT)')
    con.executemany('INSERT INTO users VALUES (?)', [('ada',), ('bob',)])
    name = "x' OR '1'='1"
    unsafe = con.execute(f"SELECT name FROM users WHERE name = '{name}'").fetchall()
    safe = con.execute('SELECT name FROM users WHERE name = ?', (name,)).fetchall()
    print('formatted:', unsafe)
    print('placeholder:', safe)
Output
formatted: [('ada',), ('bob',)]
placeholder: []

The context manager rolls back when the body raises an exception (rule r6):

import contextlib
import sqlite3

with contextlib.closing(sqlite3.connect(':memory:')) as con:
    con.execute('CREATE TABLE t(x INTEGER)')
    try:
        with con:
            con.execute('INSERT INTO t VALUES (1)')
            raise RuntimeError('stop')
    except RuntimeError:
        pass
    print(con.execute('SELECT count(*) FROM t').fetchone())
Output
(0,)

Pitfalls

Do not build SQL with f-strings, % or + from values that a user gives (rule r5). Use the placeholders of the module (example injection).

The placeholder depends on the module (rule r3). sqlite3 uses ?, and a module with the format or pyformat style uses %s. Read the paramstyle of the module. The %s of such a module is a placeholder, not Python string formatting.

with con: does not close the connection (rule r6). Close it, or use contextlib.closing, as each example on this page does.

Comparison

"Package sql provides a generic interface around SQL (or SQL-like) databases."5 "The sql package must be used in conjunction with a database driver."5 Go has the generic interface in the standard library. "Register makes a database driver available by the provided name."5 In Python, PEP 249 is a specification, and each module implements it on its own (rule r1).

References

  1. PEP 249: Python Database API Specification v2.0, Introduction; Globals, paramstyle; Cursor Objects
  2. sqlite3, DB-API 2.0 interface for SQLite databases, introduction; How to use placeholders to bind values in SQL queries; How to use the connection context manager
  3. Overview, SQLAlchemy 2.0 Documentation, Overview
  4. Psycopg 3 documentation, introduction
  5. Package sql, Overview

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source