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()

 

posted @ 2016-11-30 10:12  Brian Tan  阅读(261)  评论(0编辑  收藏  举报