Python中是否有可用于SQLite的轻量级数据库包装器。我想像Django的ORM,但我可以指向一个数据库文件,它将为我创建所需的API(即处理所有的CRUD)。
答案 0 :(得分:46)
是的,SQLAlchemy很棒,但还有其他选择。其中一个是Peewee。 非常轻巧,可以完美地满足您的需求。
答案 1 :(得分:12)
SQLAlchemy可能正是您所寻找的。
答案 2 :(得分:8)
答案 3 :(得分:3)
绝对是peewee。我尝试了sqlalchemy,但它很乱,没有魔法。
其他ORM不再开发或不太好,如SQLobject,Elixir(sqlalchemy上面的层),PonyORM。 Peewee是迄今为止我在python社区中见过的最好的,并且它更接近ruby或php的主要ORM。
Peewee也有很多宝石,比如这个简单的快捷方式
Person.get_or_create(name='Foo', surname='Bar')
自动接受名为' Foo Bar'来自数据库(如果存在),否则创建它。
答案 4 :(得分:1)
刚刚完成了这个非常简单和酷的python面向对象的orm层。
答案 5 :(得分:0)
您可以结帐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。
答案 6 :(得分:0)
免责声明:我与软件包sql30有联系,在此处建议。
sql30可能是SQLITE
数据库上轻量级CRUD操作的另一个潜在选择。它是权重为零的ORM,仅使用本机python构造编写,并且不依赖于任何其他模块,这使其成为对Python支持有限或落后的平台(例如ESX)的绝佳选择。
对于用例(此处有疑问),用户可以简单地
创建引用数据库文件的Class对象
使用简单的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()
只要在同一线程中使用数据库连接,就可以并行操作并对其进行良好的处理,这实际上是sqlite
模块的要求。例如here。