sqlalchemy

sqlalchemy

https://www.sqlalchemy.org/

The Python SQL Toolkit and Object Relational Mapper

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

 

Overview

https://docs.sqlalchemy.org/en/14/intro.html

The SQLAlchemy SQL Toolkit and Object Relational Mapper is a comprehensive set of tools for working with databases and Python. It has several distinct areas of functionality which can be used individually or combined together. Its major components are illustrated below, with component dependencies organized into layers:

sqlalchemy

Above, the two most significant front-facing portions of SQLAlchemy are the Object Relational Mapper (ORM) and the Core.

Core contains the breadth of SQLAlchemy’s SQL and database integration and description services, the most prominent part of this being the SQL Expression Language.

 

https://docs.sqlalchemy.org/en/14/tutorial/index.html#unified-tutorial

SQLAlchemy Core is the foundational architecture for SQLAlchemy as a “database toolkit”. The library provides tools for managing connectivity to a database, interacting with database queries and results, and programmatic construction of SQL statements.

Sections that have a dark blue border on the right will discuss concepts that are primarily Core-only; when using the ORM, these concepts are still in play but are less often explicit in user code.

 

SQLAlchemy ORM builds upon the Core to provide optional object relational mapping capabilities. The ORM provides an additional configuration layer allowing user-defined Python classes to be mapped to database tables and other constructs, as well as an object persistence mechanism known as the Session. It then extends the Core-level SQL Expression Language to allow SQL queries to be composed and invoked in terms of user-defined objects.

Sections that have a light blue border on the left will discuss concepts that are primarily ORM-only. Core-only users can skip these.

 

Documentation

https://docs.sqlalchemy.org/en/14/intro.html

The documentation is separated into four sections:

 

ORM API

https://docs.sqlalchemy.org/en/14/orm/index.html

Here, the Object Relational Mapper is introduced and fully described. If you want to work with higher-level SQL which is constructed automatically for you, as well as automated persistence of Python objects, proceed first to the tutorial.

 

Tutorial

https://docs.sqlalchemy.org/en/14/tutorial/index.html#unified-tutorial

The tutorial will present both concepts in the natural order that they should be learned, first with a mostly-Core-centric approach and then spanning out into more ORM-centric concepts.

The major sections of this tutorial are as follows:

 
  • Establishing Connectivity - the Engine - all SQLAlchemy applications start with an Engine object; here’s how to create one.

  • Working with Transactions and the DBAPI - the usage API of the Engine and its related objects Connection and Result are presented here. This content is Core-centric however ORM users will want to be familiar with at least the Result object.

  • Working with Database Metadata - SQLAlchemy’s SQL abstractions as well as the ORM rely upon a system of defining database schema constructs as Python objects. This section introduces how to do that from both a Core and an ORM perspective.

  • Working with Data - here we learn how to create, select, update and delete data in the database. The so-called CRUD operations here are given in terms of SQLAlchemy Core with links out towards their ORM counterparts. The SELECT operation that is introduced in detail at Selecting Rows with Core or ORM applies equally well to Core and ORM.

  • Data Manipulation with the ORM covers the persistence framework of the ORM; basically the ORM-centric ways to insert, update and delete, as well as how to handle transactions.

  • Working with Related Objects introduces the concept of the relationship() construct and provides a brief overview of how it’s used, with links to deeper documentation.

  • Further Reading lists a series of major top-level documentation sections which fully document the concepts introduced in this tutorial.

 

 

https://docs.sqlalchemy.org/en/14/tutorial/orm_data_manipulation.html

Data Manipulation with the ORM

The previous section Working with Data remained focused on the SQL Expression Language from a Core perspective, in order to provide continuity across the major SQL statement constructs. This section will then build out the lifecycle of the Session and how it interacts with these constructs.

Prerequisite Sections - the ORM focused part of the tutorial builds upon two previous ORM-centric sections in this document:

 

Some Term

Data Mapper Pattern

https://www.js-data.io/docs/data-mapper-pattern#:~:text=The%20Data%20Mapper%20Pattern%20is%20an%20architectural%20pattern,data%20between%20objects%20in%20memory%20and%20persistent%20storage.

The Data Mapper Pattern is an architectural pattern introduced by Martin Fowler in his book Patterns of Enterprise Application Architecture. A Data Mapper is a type of Data Access Layer that performs bi-directional transfer of data between objects in memory and persistent storage. With the Data Mapper Pattern, the in-memory objects have no idea there is a database, and the database schema is unaware of any objects that use it. This separation is the main purpose of a Data Mapper.

 

Relational Algebra

https://www.tutorialspoint.com/dbms/relational_algebra.htm

Relational algebra is a procedural query language, which takes instances of relations as input and yields instances of relations as output. It uses operators to perform queries. An operator can be either unary or binary. They accept relations as their input and yield relations as their output. Relational algebra is performed recursively on a relation and intermediate results are also considered relations.

The fundamental operations of relational algebra are as follows −

  • Select
  • Project
  • Union
  • Set different
  • Cartesian product
  • Rename

 

 

Code

https://github.com/michaelliao/learn-python3/blob/master/samples/db/do_sqlalchemy.py

 

https://www.liaoxuefeng.com/wiki/1016959663602400/1017803857459008

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# 创建对象的基类:
Base = declarative_base()

# 定义User对象:
class User(Base):
    # 表的名字:
    __tablename__ = 'user'

    # 表的结构:
    id = Column(String(20), primary_key=True)
    name = Column(String(20))

# 初始化数据库连接:
engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)

# 创建session对象:
session = DBSession()
# 创建新User对象:
new_user = User(id='5', name='Bob')
# 添加到session:
session.add(new_user)
# 提交即保存到数据库:
session.commit()
# 关闭session:
session.close()

# 创建Session:
session = DBSession()
# 创建Query查询,filter是where条件,最后调用one()返回唯一行,如果调用all()则返回所有行:
user = session.query(User).filter(User.id=='5').one()
# 打印类型和对象的name属性:
print('type:', type(user))
print('name:', user.name)
# 关闭Session:
session.close()

 

 

TP - TUTORIAL

https://www.tutorialspoint.com/sqlalchemy/sqlalchemy_quick_guide.htm

SQLAlchemy is a popular SQL toolkit and Object Relational Mapper. It is written in Python and gives full power and flexibility of SQL to an application developer. It is an open source and cross-platform software released under MIT license.

SQLAlchemy is famous for its object-relational mapper (ORM), using which, classes can be mapped to the database, thereby allowing the object model and database schema to develop in a cleanly decoupled way from the beginning.

As size and performance of SQL databases start to matter, they behave less like object collections. On the other hand, as abstraction in object collections starts to matter, they behave less like tables and rows. SQLAlchemy aims to accommodate both of these principles.

For this reason, it has adopted the data mapper pattern (like Hibernate) rather than the active record pattern used by a number of other ORMs. Databases and SQL will be viewed in a different perspective using SQLAlchemy.

Michael Bayer is the original author of SQLAlchemy. Its initial version was released in February 2006. Latest version is numbered as 1.2.7, released as recently as in April 2018.

 

上一篇:2021-11-14


下一篇:模型类 自定义管理器