Databases

https://d33wubrfki0l68.cloudfront.net/a885c8c4140bfc78f792ab2686d499676a25390b/b48f7/_images/33907152464_a99fdcc8de_k_d.jpg

DB-API

The Python Database API (DB-API) defines a standard interface for Pythondatabase access modules. It’s documented in PEP 249.Nearly all Python database modules such as sqlite3, psycopg, andmysql-python conform to this interface.

Tutorials that explain how to work with modules that conform to this interface can be foundhere andhere.

SQLAlchemy

SQLAlchemy is a commonly used database toolkit.Unlike many database libraries it not only provides an ORM layer but also ageneralized API for writing database-agnostic code without SQL.

  1. $ pip install sqlalchemy

Records

Records is minimalist SQL library,designed for sending raw SQL queries to various databases. Data can be usedprogrammatically or exported to a number of useful data formats.

  1. $ pip install records

Also included is a command-line tool for exporting SQL data.

Django ORM

The Django ORM is the interface used by Djangoto provide database access.

It’s based on the idea ofmodels,an abstraction that makes it easier to manipulate data in Python.

The basics:

  • Each model is a Python class that subclasses django.db.models.Model.
  • Each attribute of the model represents a database field.
  • Django gives you an automatically-generated database-access API; seeMaking queries.

peewee

peewee is another ORM with a focuson being lightweight with support for Python 2.6+ and 3.2+ which supportsSQLite, MySQL, and PostgreSQL by default. Themodel layeris similar to that of the Django ORM and it hasSQL-like methodsto query data. While SQLite, MySQL, and PostgreSQL are supported out-of-the-box,there is a collection of add-onsavailable.

PonyORM

PonyORM is an ORM that takes a different approach toquerying the database. Instead of writing an SQL-like language or booleanexpressions, Python’s generator syntax is used. There’s also a graphicalschema editor that can generate PonyORM entities for you. It supports Python2.6+ and Python 3.3+ and can connect to SQLite, MySQL, PostgreSQL, and Oracle.

SQLObject

SQLObject is yet another ORM. It supports a widevariety of databases: common database systems like MySQL, PostgreSQL, and SQLite andmore exotic systems like SAP DB, SyBase, and Microsoft SQL Server. It only supports Python 2from Python 2.6 upwards.

原文: https://docs.python-guide.org/scenarios/db/