python数据库/sql编程-从哪里开始[英] python database / sql programming - where to start




  1. 应该是小的足迹.我每个表只需要几千张记录.每个数据库只有少数表.
  2. 如果是由Python默认安装提供的,那么很棒.必须是开源,在Windows和Linus上可用.
  3. 最好如果不是直接编写SQL,但是完全不需要ORM.可以使我免受实际数据库的影响,但并不是那么重要的库.与ADO相似的东西会很棒.
  4. 大多将通过代码使用,但是如果有GUI前端,那很棒
  5. 只需要几页即可开始.我不想阅读阅读表以及选择语句的工作方式.我知道所有这些.
  6. 对Python 3的支持是首选,但是2.x也可以.


这是一个100ish Line Python脚本,可以收集有关相对数量的文件(例如10K)的数据,并创建有关它们的元数据文件,然后是一个有关整个文件树的大型元数据文件.我只需要避免重新处理已经处理的文件,并为更新的文件创建元数据,并更新主元数据.在某种程度上,缓存处理后的数据,仅在文件更新上进行更新. 如果缓存损坏/不可用,则只需处理整个树即可.可能需要20分钟,但这没关系.


我想避免任何外部依赖项,以便可以轻松地将脚本放在任何系统上,并在其上安装Python.作为Windows,有时很难安装所有组件. 因此,我认为,即使数据库也可能是过度杀伤.





每个人都推荐一个ORM层.如果您确实需要数据库,那是非常有道理的.好吧,这是标题中要求的: - )

  1. sqlalchemy
  2. 秋天
  3. django orm
  4. 使用sqlite官方支持 pysqlite
  5. storm
  6. elixir
  7. 只使用Python自己的泡菜





这给我留下了一个orm,可以推荐:秋天.非常轻巧,与Sqlite一起使用.如果您的嵌入式应用程序将是多线程,那么您绝对需要秋天;它具有支持多线程SQLite的扩展. (全面披露:我写了这些扩展名并贡献了它们.我在为Realnetworks工作时写了它们,我的老板允许我捐赠它们,因此公开感谢Realnetworks.)




我从这里开始: in-python




What is the best way to use an embedded database, say sqlite in Python:

  1. Should be small footprint. I'm only needing few thousands records per table. And just a handful of tables per database.
  2. If it's one provided by Python default installation, then great. Must be open-source, available on Windows and Linus.
  3. Better if SQL is not written directly, but no ORM is fully needed. Something that will shield me from the actual database, but not that huge of a library. Something similar to ADO will be great.
  4. Mostly will be used through code, but if there is a GUI front end, then that is great
  5. Need just a few pages to get started with. I don't want to go through pages reading what a table is and how a Select statement works. I know all of that.
  6. Support for Python 3 is preferred, but 2.x is okay too.

The usage is not a web app. It's a small database to hold at most 5 tables. The data in each table is just a few string columns. Think something just larger than a pickled dictionary

Update: Many thanks for the great suggestions.
The use-case I'm talking about is fairly simple. One you'd probably do in a day or two.
It's a 100ish line Python script that gathers data about a relatively large number of files (say 10k), and creates metadata files about them, and then one large metadata file about the whole files tree. I just need to avoid re-processing the files already processed, and create the metadata for the updated files, and update the main metadata file. In a way, cache the processed data, and only update it on file updates. If the cache is corrupt / unavailable, then simply process the whole tree. It might take 20 minutes, but that's okay.

Note that all processing is done in-memory.

I would like to avoid any external dependencies, so that the script can easily be put on any system with just a Python installation on it. Being Windows, it is sometimes hard to get all the components installed. So, In my opinion, even a database might be an overkill.

You probably wouldn't fire up an Office Word/Writer to write a small post it type note, similarly I am reluctant on using something like Django for this use-case.

Where to start?


This is an aggregate of answers, in no particular order:

Everybody is recommending an ORM layer. Which makes perfect sense, if you really need a database. Well, that was sort of requested in the title :-)

  1. SQLAlchemy
  2. Autumn
  3. Django ORM
  4. Use SQLite official support Pysqlite
  5. Storm
  6. Elixir
  7. Just use Python's own Pickle

But I'm starting to think that if an in-memory database is sufficient, in this will be used in scripts only, not a web app or even a desktop gui, then option 7 is also perfectly valid, provided no transaction support is needed and "database" integrity is not an issue.


I highly recommend the use of a good ORM. When you can work with Python objects to manage your database rows, life is so much easier.

I am a fan of the ORM in Django. But that was already recommended and you said that is too heavyweight.

This leaves me with exactly one ORM to recommend: Autumn. Very lightweight, works great with SQLite. If your embedded application will be multithreaded, then you absolutely want Autumn; it has extensions to support multithreaded SQLite. (Full disclosure: I wrote those extensions and contributed them. I wrote them while working for RealNetworks, and my bosses permitted me to donate them, so a public thank-you to RealNetworks.)

Autumn is written in pure Python. For SQLite, it uses the Python official SQLite module to do the actual SQL stuff. The memory footprint of Autumn itself is tiny.

I do not recommend APSW. In my humble opinion, it doesn't really do very much to help you; it just provides a way to execute SQL statements, and leaves you to master the SQL way of doing things. Also, it supports every single feature of SQLite, even the ones you rarely use, and as a result it actually has a larger memory footprint than Autumn, while not being as easy to use.


I started here:

It's 5 (short) pages with just the essentials got me going right away.