本文整理汇总了Python中pony.orm.Database方法的典型用法代码示例。如果您正苦于以下问题:Python orm.Database方法的具体用法?Python orm.Database怎么用?Python orm.Database使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pony.orm
的用法示例。
在下文中一共展示了orm.Database方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_common_pony
# 需要导入模块: from pony import orm [as 别名]
# 或者: from pony.orm import Database [as 别名]
def test_common_pony():
db = orm.Database()
class Account(db.Entity):
name = orm.Required(str)
msgn_id = orm.Required(int)
auth_code = orm.Required(str)
db.bind('sqlite', ':memory:', create_db=True)
db.generate_mapping(create_tables=True)
orm.sql_debug(True)
@orm.db_session
def test():
Account(name="haje01", msgn_id=3498, auth_code="1234")
a = Account(name="haje02", msgn_id=2345, auth_code="1234")
assert len(orm.select(a for a in Account)) == 2
assert len(orm.select(a for a in Account if a.msgn_id == 2345)) == 1
a.delete()
assert len(orm.select(a for a in Account)) == 1
test()
示例2: safe_db_init
# 需要导入模块: from pony import orm [as 别名]
# 或者: from pony.orm import Database [as 别名]
def safe_db_init(db, sqlite_file):
"""Safe DB binding, mapping
In automated test, multiple DB binds can occur, and cause
"Database object already bound" error. This function
handle error of duplicated binding.
"""
try:
db.bind('sqlite', sqlite_file, create_db=True)
except TypeError:
pass
else:
try:
db.generate_mapping(create_tables=True)
except orm.core.MappingError:
pass