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

本文是小编为大家收集整理的关于python数据库/sql编程-从哪里开始的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

使用嵌入式数据库的最佳方法是什么,例如python中的sqlite:

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

用法不是网络应用程序.这是一个最多可容纳5张表的小数据库.每个表中的数据只是几个字符串列.想到比腌词典大的东西

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

请注意,所有处理都是内存中进行的.

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

您可能不会启动办公室的单词/作家写一个小文章,类似地,我也不愿意使用django之类的东西作为此用例.

从哪里开始?

推荐答案

这是答案的汇总,没有特别的顺序:

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

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

,但我开始认为,如果一个内存数据库足够,则仅在脚本中使用,而不是Web应用程序甚至桌面GUI,则选项7也是完全有效的,只要没有交易,需要支持,"数据库"的完整性不是问题.

其他推荐答案

我强烈建议使用好ORM.当您可以使用Python对象来管理数据库行时,生活要轻松得多.

我是Django中的ORM的粉丝.但这已经被推荐了,您说这太重了.

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

秋天用纯python编写.对于SQLite,它使用Python官方SQLITE模块来执行实际的SQL工作.秋天本身的记忆足迹很小.

我不推荐APSW.以我的拙见,它并没有真正为您提供帮助.它只是提供了执行SQL语句的方法,并让您掌握SQL做事的方式.此外,它支持SQLite的每一个功能,即使是您很少使用的功能,因此它实际上具有比秋天更大的内存足迹,同时又不易于使用.

其他推荐答案

我从这里开始:

http://www.dev.dev.dev.devshed.com/c/a/python/python in-python

是5(简短的)页面,只有必需品让我立即前进.

本文地址:https://www.itbaoku.cn/post/597567.html

问题描述

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:

http://www.devshed.com/c/a/Python/Using-SQLite-in-Python

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