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
簡介:這個介紹的很好。

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