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 becoming more popular in the programming community due to its similarity to Ruby on Rails' ActiveRecord pattern. The first version of SQLObject was released in October 2002. It is licensed under LGPL.

In SQLObject, database concepts are mapped into Python in a way that's very similar to SQLAlchemy, where tables are mapped as classes, rows as instances and columns as attributes. It also provides a Python-object-based query language that makes SQL more abstract, thus providing database agnosticity for applications.

 1 $ pip install sqlobject
 2 Downloading/unpacking sqlobject
 3 Downloading SQLObject-1.5.1.tar.gz (276kB): 276kB downloaded
 4 Running setup.py egg_info for package sqlobject
 5  
 6 warning: no files found matching '*.html'
 7 warning: no files found matching '*.css'
 8 warning: no files found matching 'docs/*.html'
 9 warning: no files found matching '*.py' under directory 'tests'
10 Requirement already satisfied (use --upgrade to upgrade): FormEncode>=1.1.1 in /Users/xiaonuogantan/python2-workspace/lib/python2.7/site-packages (from sqlobject)
11 Installing collected packages: sqlobject
12 Running setup.py install for sqlobject
13 changing mode of build/scripts-2.7/sqlobject-admin from 644 to 755
14 changing mode of build/scripts-2.7/sqlobject-convertOldURI from 644 to 755
15  
16 warning: no files found matching '*.html'
17 warning: no files found matching '*.css'
18 warning: no files found matching 'docs/*.html'
19 warning: no files found matching '*.py' under directory 'tests'
20 changing mode of /Users/xiaonuogantan/python2-workspace/bin/sqlobject-admin to 755
21 changing mode of /Users/xiaonuogantan/python2-workspace/bin/sqlobject-convertOldURI to 755
22 Successfully installed sqlobject
23 Cleaning up...
 1 >>> from sqlobject import StringCol, SQLObject, ForeignKey, sqlhub, connectionForURI
 2 >>> sqlhub.processConnection = connectionForURI('sqlite:/:memory:')
 3 >>>
 4 >>> class Person(SQLObject):
 5 ...     name = StringCol()
 6 ...
 7 >>> class Address(SQLObject):
 8 ...     address = StringCol()
 9 ...     person = ForeignKey('Person')
10 ...
11 >>> Person.createTable()
12 []
13 >>> Address.createTable()
14 []

The code above created two simple tables: person and address. To create or insert records into these two tables, we simply instantiate a person and an address like normal Python objects:

1 >>> p = Person(name='person')
2 >>> a = Address(address='address', person=p)
3 >>> p
4  
5 >>> a
6 <address>

To get or retrieve the new records from the database, we use the magical q object attached to the Person and Address classes:

 1 >>> persons = Person.select(Person.q.name == 'person')
 2 >>> persons
 3  
 4 >>> list(persons)
 5 []
 6 >>> p1 = persons[0]
 7 >>> p1 == p
 8 True
 9 >>> addresses = Address.select(Address.q.person == p1)
10 >>> addresses
11  
12 >>> list(addresses)
13 [<address>]
14 >>> a1 = addresses[0]
15 >>> a1 == a
16 True

 

posted @ 2016-11-30 09:57  Brian Tan  阅读(326)  评论(0编辑  收藏  举报