数据库包装程序库
notanorm的Python项目详细描述
简单的库,使得在python中使用数据库更加方便。在
notanorm
可以返回对象而不是行,保护您不被注入,并且
有一个非常简单的驱动程序接口。在
显然不是ORM,因为ORM通常是具有 常常混淆语义。在
示例:
from notanorm import SqliteDb
from notanorm import MysqlDb
fname = ":memory:"
# default options are
db = SqliteDb(fname)
# no special create support, just use a string
db.query("create table foo (bar text, iv integer primary key)")
# insert, select, update, and upsert are convenient and do the right thing
# preventing injection, normalizing across db types, etc.
db.insert(bar="hi", iv=1)
db.insert(bar="hi", iv=2)
db.insert(bar="ho", iv=3)
db.upsert(bar="ho", iv=4) # primary keys are required for upserts
db.select("foo", bar="hi")[0].bar # hi
db.select("foo", {"bar": "hi"})[0].iv # 1
db.count("foo", bar="hi") # 2
class Foo:
def __init__(self, bar=None, iv=None):
self.bar = bar
self.iv = iv
# create a class during select
db.register_class("foo", Foo)
obj = db.select_one("foo", bar="hi")
print(obj.bar) # hi
- 项目
标签: