Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy
SQLAlchemy
SQLAlchemy is an open source SQL toolkit and ORM for the Python programming language released under the MIT license. It was released initially in February 2006 and written by Michael Bayer. 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". It has adopted the data mapper pattern (like Hibernate in Java) rather than the active record pattern (like the one in Ruby on Rails).
SQLAlchemy's unit-of-work principal makes it essential to confine all the database manipulation code to a specific database session that controls the life cycles of every object in that session. Similar to other ORMs, we start by defining subclasses of declarative_base()
in order to map tables to Python classes.
>>> from sqlalchemy import Column, String, Integer, ForeignKey
>>> from sqlalchemy.orm import relationship
>>> from sqlalchemy.ext.declarative import declarative_base
>>>
>>> Base = declarative_base()
>>>
>>>
>>> class Person(Base):
... __tablename__ = 'person'
... id = Column(Integer, primary_key=True)
... name = Column(String)
...
>>>
>>> class Address(Base):
... __tablename__ = 'address'
... id = Column(Integer, primary_key=True)
... address = Column(String)
... person_id = Column(Integer, ForeignKey(Person.id))
... person = relationship(Person)
Before we write any database code, we need to create an database engine for our db session.
>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite:///')
Once we have created a database engine, we can proceed to create a database session and create tables for all the database classes previously defined as Person
and Address
.
>>> from sqlalchemy.orm import sessionmaker
>>> session = sessionmaker()
>>> session.configure(bind=engine)
>>> Base.metadata.create_all(engine)
Now the session
object becomes our unit-of-work constructor and all the subsequent database manipulation code and objects will be attached to a db session constructed by calling its __init__()
method.
>>> s = session()
>>> p = Person(name='person')
>>> s.add(p)
>>> a = Address(address='address', person=p)
>>> s.add(a)
To get or retrieve the database objects, we call query()
and filter()
methods from the db session object.
>>> p = s.query(Person).filter(Person.name == 'person').one()
>>> p >>> print "%r, %r" % (p.id, p.name)
1, 'person'
>>> a = s.query(Address).filter(Address.person == p).one()
>>> print "%r, %r" % (a.id, a.address)
1, 'address'
Notice that so far we haven't committed any changes to the database yet so that the new person and address objects are not actually stored in the database yet. Calling s.commit()
will actually commit the changes, i.e., inserting a new person and a new address, into the database.
>>> s.commit()
>>> s.close()
Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy的更多相关文章
- Python’s SQLAlchemy vs Other ORMs[转发 7] 比较结论
Comparison Between Python ORMs For each Python ORM presented in this article, we are going to list t ...
- Python’s SQLAlchemy vs Other ORMs[转发 0]
原文地址:http://pythoncentral.io/sqlalchemy-vs-orms/ Overview of Python ORMs As a wonderful language, Py ...
- Python’s SQLAlchemy vs Other ORMs[转发 3]Django's ORM
Django's ORM Django is a free and open source web application framework whose ORM is built tightly i ...
- Python’s SQLAlchemy vs Other ORMs[转发 2]Storm
Storm Storm is a Python ORM that maps objects between one or more databases and Python. It allows de ...
- Python’s SQLAlchemy vs Other ORMs[转发 1]SQLObject
SQLObject SQLObject is a Python ORM that maps objects between a SQL database and Python. It is becom ...
- Python’s SQLAlchemy vs Other ORMs[转发 4]peewee
peewee peewee is a small, expressive ORM. Compared to other ORMs, peewee focuses on the principal of ...
- Python’s SQLAlchemy vs Other ORMs[转发 5] PonyORM
PonyORM PonyORM allows you to query the database using Python generators. These generators are trans ...
- Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- Python操作Redis、Memcache、RabbitMQ、SQLAlchemy
Python操作 Redis.Memcache.RabbitMQ.SQLAlchemy redis介绍:redis是一个开源的,先进的KEY-VALUE存储,它通常被称为数据结构服务器,因为键可以包含 ...
随机推荐
- Weblogic日志机制详解
服务器日志 每个 WebLogic Server 实例将来自子系统和应用程序的所有消息写入位于本地主机上的服务器日志文件.默认情况下,服务器日志文件位于服务器实例根目录下的 logs 目录中:例如, ...
- ecshop二次开发之常用函数及汇总
lib_time.php gmtime()说明:获得当前格林威治时间的时间戳 server_timezone()说明:获得服务器的时区 local_mktime($hour = NULL , $min ...
- mysql登陆问题
1.centos6下mysql5.7的登陆问题 问题的描述:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using p ...
- Tempter of the Bone
Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he ...
- 图片使用base64展示代码,后台为jfinal
前台使用ajax获取数据,下面步骤为把图片对应的id获取到,然后判断是否为空,不为空则发送请求获取数据,数据为base64数据格式: img需要注明数据类型格式:即data:image/jpg:bas ...
- 关键字nullable,nonnull,null_resettable,_Null_unspecified详解
相信在开发过程中,很多小伙伴们儿都会见到nullable,nonnull,null_resettable,_Null_unspecified这几个关键字,但是并不知道它们是什么意思,下面我就给大家一一 ...
- SQL表连接查询(inner join、full join、left join、right join)
SQL表连接查询(inner join.full join.left join.right join) 前提条件:假设有两个表,一个是学生表,一个是学生成绩表. 表的数据有: 一.内连接-inner ...
- HTML空格标签
双学位论文+本学位开题报告+实训项目+实训考试+学员考试,真是脑子都要爆炸... 话不多说,留个小知识点,HTML的空格替代符号表示法: 1. 半个空白,1个字符宽度: 或者 2.1个空白,2个 ...
- 第七章 Hibernate性能优化
一对一关联 实体类关系 一对多 多对多 一对一 Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射.下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方 ...
- shell技巧
如何实现Shell脚本以DEAMON的方式运行,即实现Shell版的Fork if [ "$1" != 'background' ] ; then scriptdir=$(cd & ...