Skip to Content
Essential SQLAlchemy, 2nd Edition
book

Essential SQLAlchemy, 2nd Edition

by Jason Myers, Rick Copeland
May 2025
Intermediate to advanced
208 pages
2h 55m
Japanese
O'Reilly Media, Inc.
Content preview from Essential SQLAlchemy, 2nd Edition

第3章 例外とトランザクション 例外と取引

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

前章では、1つの文の中でデータを扱うことが多く、エラーになるようなことは避けた。 この章では、発生するエラーの種類とその対処法を知るために、いくつかのアクションをわざと間違えて実行する。この章の最後には、成功させる必要のある文のグループをトランザクションにグループ化し、グループが正しく実行されるか、正しくクリーンアップされるようにする方法を学ぶ。まず、爆破することから始めよう!

例外

AttributeErrors SQLAlchemy で発生しうる例外は数多くあるが、ここでは最も一般的なものに焦点を当て る。IntegrityErrors.これらの一般的な例外の扱い方を学ぶことで、発生頻度の低い例外に対処する準備ができ るようになる。

この章に従うために、新しいPythonシェルを起動し、第1章で構築したテーブルをシェルにロードしておくこと。例3-1には、これらのテーブルと接続が含まれている。

例3-1. シェル環境をセットアップする
from datetime import datetime

from sqlalchemy import (MetaData, Table, Column, Integer, Numeric, String,
                        DateTime, ForeignKey, Boolean, create_engine,
                        CheckConstraint)
metadata = MetaData()

cookies = Table('cookies', metadata,
    Column('cookie_id', Integer(), primary_key=True),
    Column('cookie_name', String(50), index=True),
    Column('cookie_recipe_url', String(255)),
    Column('cookie_sku', String(55)),
    Column('quantity', Integer()),
    Column('unit_cost', Numeric(12, 2)),
    CheckConstraint('quantity > 0', name='quantity_positive')
)

users = Table('users', metadata,
    Column('user_id', Integer(), primary_key=True),
    Column('username', String(15), nullable=False, unique=True),
    Column('email_address', String(255), nullable=False),
    Column('phone', String(20), nullable=False),
    Column('password', String(25), nullable=False),
    Column('created_on', DateTime(), default=datetime.now),
    Column('updated_on', DateTime(), default=datetime.now, onupdate=datetime.now)
)

orders = Table('orders', metadata ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Essential SQLAlchemy, 2nd Edition

Essential SQLAlchemy, 2nd Edition

Jason Myers, Rick Copeland
High Performance Python, 3rd Edition

High Performance Python, 3rd Edition

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341649866