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.

 1 >>> from sqlalchemy import Column, String, Integer, ForeignKey
 2 >>> from sqlalchemy.orm import relationship
 3 >>> from sqlalchemy.ext.declarative import declarative_base
 4 >>>
 5 >>> Base = declarative_base()
 6 >>>
 7 >>>
 8 >>> class Person(Base):
 9 ...     __tablename__ = 'person'
10 ...     id = Column(Integer, primary_key=True)
11 ...     name = Column(String)
12 ...
13 >>>
14 >>> class Address(Base):
15 ...     __tablename__ = 'address'
16 ...     id = Column(Integer, primary_key=True)
17 ...     address = Column(String)
18 ...     person_id = Column(Integer, ForeignKey(Person.id))
19 ...     person = relationship(Person)

Before we write any database code, we need to create an database engine for our db session.

1 >>> from sqlalchemy import create_engine
2 >>> 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.

1 >>> from sqlalchemy.orm import sessionmaker
2 >>> session = sessionmaker()
3 >>> session.configure(bind=engine)
4 >>> 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.

1 >>> s = session()
2 >>> p = Person(name='person')
3 >>> s.add(p)
4 >>> a = Address(address='address', person=p)
5 >>> s.add(a)

To get or retrieve the database objects, we call query() and filter() methods from the db session object.

 

1 >>> p = s.query(Person).filter(Person.name == 'person').one()
2 >>> p
3  
4 >>> print "%r, %r" % (p.id, p.name)
5 1, 'person'
6 >>> a = s.query(Address).filter(Address.person == p).one()
7 >>> print "%r, %r" % (a.id, a.address)
8 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.

1 >>> s.commit()
2 >>> s.close()

 

转载于:https://www.cnblogs.com/tanxstar/p/6116543.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值