
- Python Pyramid - Home
- Python Pyramid - Overview
- Pyramid - Environment Setup
- Python Pyramid - Hello World
- Pyramid - Application Configuration
- Python Pyramid - Url Routing
- Python Pyramid - View Configuration
- Python Pyramid - Route Prefix
- Python Pyramid - Templates
- Pyramid - HTML Form Template
- Python Pyramid - Static Assets
- Python Pyramid - Request Object
- Python Pyramid - Response Object
- Python Pyramid - Sessions
- Python Pyramid - Events
- Python Pyramid - Message Flashing
- Pyramid - Using SQLAlchemy
- Python Pyramid - Cookiecutter
- Python Pyramid - Creating A Project
- Python Pyramid - Project Structure
- Python Pyramid - Package Structure
- Creating A Project Manually
- Command Line Pyramid
- Python Pyramid - Testing
- Python Pyramid - Logging
- Python Pyramid - Security
- Python Pyramid - Deployment
- Python Pyramid Useful Resources
- Python Pyramid - Quick Guide
- Python Pyramid - Useful Resources
- Python Pyramid - Discussion
Python Pyramid - Using SQLAlchemy
In this chapter, we shall learn how to use a relational database as a back-end with the Pyramid web application. Python can interact with almost every relational database using corresponding DB-API compatible connector modules or drivers. However, we shall use SQLAlchemy library as an interface between Python code and a database (we are going to use SQLite database as Python has in-built support for it). SQLAlchemy is a popular SQL toolkit and Object Relational Mapper.
Object Relational Mapping is a programming technique for converting data between incompatible type systems in object-oriented programming languages. Usually, the type system used in an Object Oriented language like Python contains non-scalar types. However, data types in most of the database products such as Oracle, MySQL, etc., are of primitive types such as integers and strings.
In an ORM system, each class maps to a table in the underlying database. Instead of writing tedious database interfacing code yourself, an ORM takes care of these issues for you while you can focus on programming the logics of the system.
In order to use SQLALchemy, we need to first install the library using PIP installer.
pip install sqlalchemy
SQLAlchemy is designed to operate with a DBAPI implementation built for a particular database. It uses dialect system to communicate with various types of DBAPI implementations and databases. All dialects require that an appropriate DBAPI driver is installed.
The following are the dialects included −
Firebird
Microsoft SQL Server
MySQL
Oracle
PostgreSQL
SQLite
Sybase
Database Engine
Since we are going to use SQLite database, we need to create a database engine for our database called test.db. Import create_engine() function from the sqlalchemy module.
from sqlalchemy import create_engine from sqlalchemy.dialects.sqlite import * SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args = {"check_same_thread": False})
In order to interact with the database, we need to obtain its handle. A session object is the handle to database. Session class is defined using sessionmaker() - a configurable session factory method which is bound to the engine object.
from sqlalchemy.orm import sessionmaker, Session session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Next, we need a declarative base class that stores a catalog of classes and mapped tables in the Declarative system.
from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()
Model Class
Students, a subclass of Base, is mapped to a students table in the database. Attributes in the Students class correspond to the data types of the columns in the target table. Note that the id attribute corresponds to the primary key in the book table.
class Students(Base): __tablename__ = 'student' id = Column(Integer, primary_key=True, nullable=False) name = Column(String(63), unique=True) marks = Column(Integer) Base.metadata.create_all(bind=engine)
The create_all() method creates the corresponding tables in the database. It can be confirmed by using a SQLite Visual tool such as SQLiteStudio.

We shall now define view functions for performing CRUD operations (i.e. add, display, modify and delete rows) on the student table in the above database.
Add a New Student Record
First, we shall create a HTML form template for the user to enter student data and define a view that renders the template. Here is the myform.html template