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存储,它通常被称为数据结构服务器,因为键可以包含 ...
随机推荐
- ubuntu安装cpu版caffe
最近在笔记本上配置了ubuntu14.04,并配置了caffe,整个过程大概花了2个小时. 希望在安装时能给大家一个启发,这里配置的是无gpu版的,因为我的笔记本时核心显卡,配置gpu版的要编译cud ...
- markdown-js 添加表格,代码块 parse
简介 markdown-js 是将 markdown转换成 HTML 的 JavaScript 库,我再网站中使用它来预览 markdown ,但是发现它对 代码块 和 表格 是不转换的.这么鸡肋的地 ...
- IOS 视频缩略图的生成
使用AVFoundation框架可以生成视频缩略图,用到的类: >>AVAsset: 用于获取多媒体的相关信息,如多媒体的画面和声音等. >>AVURLAsset: AVAss ...
- ShowDoc部署手册
ShowDoc介绍 关于ShowDoc的介绍,请访问:http://blog.star7th.com/2015/11/1816.html 环境依赖 1.必需环境 PHP5.3以上版本.php-gd库 ...
- Hibernate的关联映射
单向N-1关联 <many-to-one> 单向N-1关系,比如多个人对应同一个住址,只需要从人实体端找到对应的住址实体,无须关系某个地址的全部住户.程序在N的一端增加一个属性,该属性引用 ...
- [Machine-Learning] 熟悉Matlab
浮点数取整的几个函数 floor: 向下取整 ceil: 向上取整 round: 取最接近的整数 fix: 向0取整 不等于 Matlab 中,使用~=表示不等于. 数组相关操作 使用 [] 命名数组 ...
- jira 6.3.6安装-汉化-破解
jira是是一个国外的项目管理软件,收费的,至于功能什么的这里就不具体说了,大家可以网上查看有很多描述的 首先你要在JIRA官网注册一个账户,可以有30天的试用期,网上很多教程是让你去网上搜一个密钥, ...
- Bootstrap 3 Datepicker 使用过程
最近在创建记录的时候,需要用到日历的功能.本身是使用的bootstrap布局的,所以就找到Datepicker,看了一下用起来还是挺方便的.下面就是使用过程. 依赖的资源 jQuery Moment. ...
- DllImport attribute的总结
C#有没有方法可以直接都用已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法),而不需要重新编写代码? 答案是肯定,就是通过接下来要说的 DllImport . DllImp ...
- socket(二)
作用域 1 2 3 if 1 == 1: name = 'alex' print(name) python中是没有块儿作用域的,所以python可以输出name java/c中有块级作用域, ...