python数据库/ sql编程 - 从哪里开始 [英] python database / sql programming - where to start
问题描述
使用嵌入式数据库的最佳方法是在Python中使用sqlite:
What is the best way to use an embedded database, say sqlite in Python:
- 我每表只需要几千条记录。
- 如果它是由Python默认安装提供的,那么很好。必须是开源的,在Windows和Linus上可用。
- 如果SQL不是直接写入,更好,但不完全需要ORM。某事,将屏蔽我从实际的数据库,但不是那么大的图书馆。
- 大部分将通过代码使用,但如果有一个GUI前端,那么这是很好的
- 只需要几页的开始。我不想通过阅读一个表是什么,以及Select语句如何工作。我知道所有这一切。
- 支持Python 3是首选,但是2.x也可以。
- Should be small footprint. I'm only needing few thousands records per table. And just a handful of tables per database.
- If it's one provided by Python default installation, then great. Must be open-source, available on Windows and Linus.
- 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.
- Mostly will be used through code, but if there is a GUI front end, then that is great
- 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.
- Support for Python 3 is preferred, but 2.x is okay too.
使用不是网络应用程序。它是一个小数据库,最多可容纳5个表。每个表中的数据只是几个字符串列。
更新:非常感谢您的建议。
我在说的用例是相当简单。
这是一个100行的Python脚本,它收集有关大量文件(例如10k)的数据,并创建有关它们的元数据文件,然后一个关于整个文件树的大元数据文件。我只需要避免重新处理已处理的文件,并为更新的文件创建元数据,并更新主元数据文件。在某种程度上,缓存处理的数据,并且只在文件更新时更新它。
如果缓存已损坏/不可用,那么只需处理整个树。这可能需要20分钟,但是没关系。
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.
我想避免任何外部依赖,以便脚本可以轻松地放在任何系统上只有一个Python安装上。作为Windows,有时很难安装所有组件。
所以,在我看来,即使是一个数据库可能是一个过度。
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.
您可能不会启动一个Office Word / Writer写一个小的帖子类型笔记,类似地,我不愿意使用像Django这样的用例。
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.
从哪里开始?
推荐答案
答案,没有特定的顺序:
This is an aggregate of answers, in no particular order:
每个人都在推荐一个ORM层。这使得完美的意义,如果你真的需要一个数据库。好吧,这是标题中请求的: - )
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 :-)
- SQLAlchemy
- 秋季< a>
- Django ORM
- 使用SQLite官方支持 Pysqlite
- Storm
- Elixir
- 只需使用Python自己的Pickle
- SQLAlchemy
- Autumn
- Django ORM
- Use SQLite official support Pysqlite
- Storm
- Elixir
- Just use Python's own Pickle
但我开始认为,如果一个内存数据库是足够的,在这将只在脚本,而不是一个web应用程序,甚至桌面gui,那么选项7也是完全有效的,只要没有事务需要支持,数据库完整性不是问题。
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.
这篇关于python数据库/ sql编程 - 从哪里开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!