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. js 除法 取整

    js 除法 取整 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.roun ...

  2. RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件Web业务平台

    RDIFramework.NET ━ .NET快速信息化系统开发框架  工作流程组件Web业务平台 接前两篇: RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件介 ...

  3. DNS协议

    DNS Message: Header   消息头部 Question    DNS请求 Answer  回答请求的资源记录(Resource Record(s)) Authority   指向域的资 ...

  4. HTML 5 Canvas 参考手册

    HTML 5 Canvas 参考手册 HTML 视频/音频 HTML 文档类型 描述 HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript). 不过,&l ...

  5. 学习Uml开始

    Um的全称是 Unified Modeling Language, 统一建模语言,uml可以帮助我们做软件需求和软件设计的工作, 1.1UML的定义 UML是一种通用的可视化建模语言,是一种标准化的用 ...

  6. mfc ui3

    引用:http://www.cnblogs.com/rainbowzc/archive/2010/06/02/1749872.html 本文专题讨论VC中的界面美化,适用于具有中等VC水平的读者.读者 ...

  7. 摸索js的3d全景

    先我在网上找到了一个例子,http://silali.vicp.net/three/emaple.html 完美实现3d全景,在详细查看这个例子后,发现他在手机上运行并不流畅,而且显不全并会卡顿. 我 ...

  8. jquery file upload 文件上传插件

    1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...

  9. 【转】PowerShell入门(三):如何快速地掌握PowerShell?

    转至:http://www.cnblogs.com/ceachy/archive/2013/02/01/HowToLearnPowerShell.html 如何快速地掌握PowerShell呢?总的来 ...

  10. CentOS 7下Wireshark捕获USB数据包

    1. 软件准备 安装Wireshark # yum install wireshark wireshark-gnome .csharpcode, .csharpcode pre { font-size ...