PonyORM

PonyORM allows you to query the database using Python generators. These generators are translated into SQL and the results are automatically mapped into Python objects. Writing queries as Python generators makes it easy for programmers to quickly construct certain queries.

For example, let's use PonyORM to query the previous Person and Address models in a SQLite database.

 >>> from pony.orm import Database, Required, Set
>>>
>>> db = Database('sqlite', ':memory:')
>>>
>>>
>>> class Person(db.Entity):
... name = Required(unicode)
... addresses = Set("Address")
...
>>>
>>> class Address(db.Entity):
... address = Required(unicode)
... person = Required(Person)
...
>>> db.generate_mapping(create_tables=True)

Now we have a SQLite database in memory and two tables mapped to the db object, we can insert two objects into the database.

 >>> p = Person(name="person")
>>> a = Address(address="address", person=p)
>>> db.commit()

The call db.commit() actually commits the new objects p and a into the database. Now we can query the database using the generator syntax.

 >>> from pony.orm import select
>>> select(p for p in Person if p.name == "person")[:]
[Person[1]]
>>> select(p for p in Person if p.name == "person")[:][0].name
u'person'
>>> select(a for a in Address if a.person == p)[:]
[Address[1]]
>>> select(a for a in Address if a.person == p)[:][0].address
u'address'

Python’s SQLAlchemy vs Other ORMs[转发 5] PonyORM的更多相关文章

  1. 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 ...

  2. Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy

    SQLAlchemy SQLAlchemy is an open source SQL toolkit and ORM for the Python programming language rele ...

  3. Python’s SQLAlchemy vs Other ORMs[转发 0]

    原文地址:http://pythoncentral.io/sqlalchemy-vs-orms/ Overview of Python ORMs As a wonderful language, Py ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 基于Python的SQLAlchemy的操作

    安装 在Python使用SQLAlchemy的首要前提是安装相应的模块,当然作为python的优势,可以到python安装目录下的scripts下,同时按住shift+加上鼠标左键,从而在菜单中打开命 ...

  9. SQLAlchemy(1) -- Python的SQLAlchemy和ORM

    Python的SQLAlchemy和ORM(object-relational mapping:对象关系映射) web编程中有一项常规任务就是创建一个有效的后台数据库.以前,程序员是通过写sql语句, ...

随机推荐

  1. Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形) .

    1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, ...

  2. ArrayBlockingQueue,BlockingQueue分析

    BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会造成阻塞,当容量为空时取元素操作会 ...

  3. Nhiberate (二) 搭项目

    使用: visual studio 2015 ;SQL SERVER 2012. 参考.测试可用 其中有点不太一样的地儿, ISession 的泛型方法: 用了 QueryOver<>,转 ...

  4. iOS,iPhone各机型设备号,屏幕宽高,屏幕模式

    //获取设备型号 NSString *DeviceModel= [[UIDevice currentDevice] model]; //获取设备系统版本号 NSString *DeviceIOSVer ...

  5. Force StyleCop to Ignore a File

    You can quickly force StyleCop to ignore files in a project by manually modifying the project file, ...

  6. Oracle 存储过程异常处理

    Oracle 存储过程异常处理 1.异常的优点    如果没有异常,在程序中,应当检查每个命令的成功还是失败,如  BEGIN  SELECT ...  -- check for ’no data f ...

  7. cat *.txt | grep '>' | wc -l

    find the line where the '>' located, and make a statistic.

  8. php组成数组

    每次向数据库取5条数据,不足5条就不显示 $z = (int)(count($data) / 5);for ($ii = 1; $ii <= $z; $ii++) { foreach ($dat ...

  9. jQ HTML之捕获 设置 元素添加与删除

    捕获 设置修改 添加元素 删除元素

  10. 在linux中安装adb和fastboot工具

    我用的是archlinux,在官方的软件仓库里就可以找到对应的包,包的名字叫:android-tools 据说debian系列的软件包是两个,分别是:android-tools-adb, androi ...