Python’s SQLAlchemy vs Other ORMs[转发 1]SQLObject

本文介绍了SQLObject,一种用于Python的ORM工具,它通过模仿Ruby on Rails的ActiveRecord模式简化了SQL数据库操作。文章详细展示了如何安装SQLObject,并通过创建Person和Address两个类来演示其基本用法。

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

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值