python database / sql programming - where to start

前端 未结 9 2051
闹比i
闹比i 2021-02-02 03:25

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 ha
相关标签:
9条回答
  • 2021-02-02 04:16

    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.

    0 讨论(0)
  • 2021-02-02 04:16

    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.

    0 讨论(0)
  • 2021-02-02 04:21

    What you're looking for is SQLAlchemy, which is fast becoming the de facto standard Python data access layer. To make your first experiences with SQLAlchemy even easier, check out Elixir, which is a thin ActiveRecord-style wrapper around SQLAlchemy.

    Update: Reread the question and saw the bit about not needing a full ORM. I'd still suggest going the SQLAlchemy route, just because it gives you a ridiculously easy way to work with databases in Python that you can reuse for any kind of database. Time spent working directly with SQLite is wasted once you need to connect to Oracle or something.

    0 讨论(0)
提交回复
热议问题