Python中的ORM

ORM不是Python独享。因为我使用Python的时间更久,这里主要谈Python 中的ORM.

经历了这么多事情,现在洁癖性的极力推崇某一种技术,或者极力反对某一种技术的已经比较少了。

在Python 中,根据ORM 的实现层次,ORM 至少可以有3类:

  • SQLAlchemy
    python中ORM的领军人物。

    • peewee
      peewee 一个轻量级的ORM。
  • Python-sql
    对object–>sql 做了轻量级封装。
    https://github.com/tyba/python-sql

  • torndb
    前生是tornado 的tornado.database。 对SQL 中的query, parameter 做了封装。

    • MySQLdb, pymysql…
      这就是比较底层的数据库连接使用了。

对我自己来讲,比较喜欢片底层的orm。 torndb 是我的最爱。

扩展阅读

ORM的优缺点
http://blog.csdn.net/sgear/article/details/7408251

peewee 一个轻量级的ORM (三)
http://www.csdn123.com/html/exception/706/706727_706724_706720.htm

官方文档:
http://peewee.readthedocs.org/en/latest/peewee/querying.html
简介:

This section will cover the basic CRUD operations commonly performed on a relational database:

Model.create(), for executing INSERT queries.
Model.save() and Model.update(), for executing UPDATE queries.
Model.delete_instance() and Model.delete(), for executing DELETE queries.
Model.select(), for executing SELECT queries.

http://bbs.chinaunix.net/thread-4145746-1-1.html
简介:
你操作数据库时在用繁琐难维护的SQL?还是笨重复杂的SqlAlchemy?或者是你运气够好使用DJANGO而享受其ORM?

我曾经也是有SQlAlchemy,不过几次后难以忍受其复杂和不直观,他号称最佳ORM,但是其高级特性我都用不上,反而因其设计而用的特别别扭。后来我再三搜索和比较,觉得peewee真合我意,用了两个项目后越发感叹,peewee在项目中完全没有存在感,你不会感觉到在使用peewee,而是下意识的操作一些数据,就像python内置一样。(用SQlalchemy每次操作都会知道你在用SA)。

SQL Alchemy

SQL Alchemy Session Basics
http://docs.sqlalchemy.org/en/latest/orm/session_basics.html

SQL Alchemy Contextual/Thread-local Sessions
http://docs.sqlalchemy.org/en/latest/orm/contextual.html?highlight=scoped_session#unitofwork-contextual

用 Binds 操作多个数据库
http://docs.jinkan.org/docs/flask-sqlalchemy/binds.html#id2

Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
http://www.jb51.net/article/49789.htm
简介:这个介绍的很好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章