Bancos de dados

../_images/33907152464_a99fdcc8de_k_d.jpg

DB-API

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

Podem ser encontrados tutoriais que explicam como trabalhar com módulos que estejam em conformidade com esta interface aqui e aqui.

SQLAlchemy

O SQLAlchemy é um conjunto de ferramentas de banco de dados comum. Ao contrário de muitas bibliotecas de banco de dados, ele não só fornece uma camada ORM, mas também uma API generalizada para escrever o código agnóstico do banco de dados sem SQL.

$ pip install sqlalchemy

Records

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

$ pip install records

Também está incluída uma ferramenta de linha de comando para exportação de dados SQL.

PugSQL

PugSQL is a simple Python interface for organizing and using parameterized, handwritten SQL. It is an anti-ORM that is philosophically lo-fi, but it still presents a clean interface in Python.

$ pip install pugsql

Django ORM

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

Baseia-se na ideia de modelos, uma abstração que facilita a manipulação de dados em Python.

O básico:

  • Cada modelo é uma classe Python e que é uma subclasse de django.db.models.Model.
  • Cada atributo do modelo representa um campo de banco de dados.
  • O Django oferece uma API de acesso ao banco de dados gerada automaticamente; Making queries.

peewee

peewee is another ORM with a focus on being lightweight with support for Python 2.6+ and 3.2+ which supports SQLite, MySQL, and PostgreSQL by default. The model layer is similar to that of the Django ORM and it has SQL-like methods to query data. While SQLite, MySQL, and PostgreSQL are supported out-of-the-box, there is a collection of add-ons available.

PonyORM

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

SQLObject

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