【发布时间】:2012-02-20 00:22:40
【问题描述】:
Python 中是否有可用于 SQLite 的轻量级数据库包装器。我想要 Django 的 ORM 之类的东西,但我可以只指向一个数据库文件,它会为我生成所需的 API(即处理所有的 CRUD)。
【问题讨论】:
Python 中是否有可用于 SQLite 的轻量级数据库包装器。我想要 Django 的 ORM 之类的东西,但我可以只指向一个数据库文件,它会为我生成所需的 API(即处理所有的 CRUD)。
【问题讨论】:
免责声明:我与包有隶属关系,sql30,建议在这里。
sql30 可能是SQLITE 数据库上轻量级 CRUD 操作的另一个潜在选择。它是一个零权重 ORM,仅使用原生 python 结构编写,不依赖于任何其他模块,这使其成为对 Python 支持有限或滞后的平台(如 ESX)的绝佳选择。
对于这里有问题的用例,用户可以简单地
创建一个引用数据库文件的类对象
使用简单的 JSON 定义表架构(基于表的列)。
您已准备好进行任何 CRUD 操作。
一个例子如下所示。
假设数据库文件是reviews.db,其中一个表是reviews,其中包含诸如rid, header, rating and desc(iption) 等各个评论的字段。可以如下进行 CRUD 操作。
# reviews.py
from sql30 import db
class Reviews(db.Model):
PKEY = 'rid'
DB_SCHEMA = {
'db_name': 'reviews.db',
'tables': [
{
'name': 'reviews',
'fields': {
'rid': 'uuid',
'header': 'text',
'rating': 'int',
'desc': 'text'
},
'primary_key': 'rid'
}]
}
VALIDATE_BEFORE_WRITE = True
现在可以进行如下的 CRUD 操作。
>>> import os
>>> import reviews
# Create ORM layer object instance.
>>> db = reviews.Reviews()
# With this, we can create/write records to db as following.
>>> tbl = 'reviews' # select database table, you want to operate on.
>>> db.write(tbl, rid=1, header='good thing', rating=5)
>>> db.write(tbl, rid=2, header='bad thing', rating=1, desc='what a disgusting thing')
# We can then read back the records individually are as whole as shown below.
# To read all the records from a table, simply pass the table name.
>>> db.read(tbl)
[(1, 'good thing', 5, ''), (2, 'bad thing', 1, 'what a disgusting thing')]
# To read the records from table, simply pass on the condition as params.
>>> db.read(tbl, rid=1) # Get records from table WHERE rid=1
[(1, 'good thing', 5, '')]
# Get records from table WHERE rid=1 and rating=3. Note that, there is no
# restriction on the order in which condition needs to be provided. Only
# the param name should be one of the COLUMN(s) in table.
>>> db.read(tbl, rating=3, rid=1)
[]
>>> db.read(tbl, rating=5, rid=1)
[(1, 'good thing', 5, '')]
# If we try to add another record with same primary key, it will error out.
>>> db.write(tbl, rid=1, header='good thing', rating=5)
Traceback (most recent call last):
...
...
sqlite3.IntegrityError: UNIQUE constraint failed: reviews.rid
# Updating the record is also possible by providing condition for records and updated values.
>>> where = {'rid': 2}
>>> db.update(tbl, condition=where, header='average item', rating=2)
>>> db.read(tbl)
[(1, 'good thing', 5, ''), (2, 'average item', 2, 'what a disgusting thing')]
# Deleteing the records is possble with any level of filtering.
>>> db.remove(tbl, rid=1)
>>> db.read(tbl)
[(2, 'average item', 2, 'what a disgusting thing')]
# At this point, however, all of your records are being maintained by SQLITE in-memory.
# However, commit API can be used to persist them in the DB file.
>>> db.commit()
只要在同一个线程中使用 DB 连接,就允许并行操作并处理得很好,这本质上是 sqlite 模块要求。这方面的例子是here。
【讨论】:
刚刚完成了这个非常简单酷炫的python面向对象orm层。
【讨论】:
您可以结帐RabaDB。它可以说是目前最简单的界面之一。
class Human(R.Raba) :
_raba_namespace = 'test_namespace'
#Everything that is not a raba object is primitive
name = rf.Primitive()
age = rf.Primitive()
city = rf.Primitive()
#Only Cars can fit into this relation
cars = rf.Relation('Car')
#best friend can only be a human
bestFriend = rf.RabaObject('Human')
它经过优化以减少内存消耗,它支持示例查询、继承、具有调试工具,甚至允许您在需要时回退到 SQL。
【讨论】:
绝对是peewee。我已经尝试过 sqlalchemy,但它一团糟而且没有魔法。
其他 ORM 不再开发或不太好,例如 SQLobject、Elixir(sqlalchemy 之上的层)、PonyORM。 Peewee 是迄今为止我在 python 社区中看到的最好的,它更接近于 ruby 或 php 的主要 ORM。
Peewee 也有很多宝石,比如这个少数快捷方式
Person.get_or_create(name='Foo', surname='Bar')
如果存在,则自动从数据库中获取名为“Foo Bar”的人,否则创建它。
【讨论】:
是的,SQLAlchemy 很棒,但还有其他选择。其中之一是Peewee。 非常轻巧,它可能与您正在寻找的完美契合。
【讨论】:
【讨论】:
SQLAlchemy 可能是您正在寻找的。
【讨论】: