pyactiverecord是类似mysql包装器的python活动记录。
pyactiverecord的Python项目详细描述
pyactiverecord是类似mysql包装器的python活动记录。
功能
- 类似mysql包装器的活动记录
- orm
要求
- python 3.x
- mysql
- mysql连接器python
安装
pip install pyactiverecord
用法
创建模型类。
import model class SampleModel(model.Model): number = model.Column(type=model.Type.int) title = model.Column(type=modle.Type.varchar) text = model.Column(type=model.Type.text) date = model.Column(type=model.Type.timestamp)
此时,如果此表的模型类名的小写不是 存在,表自动创建。
pyactiverecord需要连接到数据库,程序需要 调用databese.setup()方法。
import model def execute(): s = SampleModel() s.title = "test" s.text = "test to save sample model" s.save() s = SampleModel.query().first() print(s.title, s.text) if __name__ == '__main__': model.Database.setup( host="localhost", database="database_name", user="root", password="" ) execute()
方法
类方法
查询:从表中提取数据
data = SampleModel.query() for d in data: print(d.title) # where data = SampleModel.query(where=["title='py-activerecord'", "text like '%Python%'"]) # order data = SampleModel.query(order=["id asc", "title desc"])
实例方法
保存:将模型对象保存到表中
s = SampleModel() s.number = 1 s.title = "py-activerecord" s.text = "ActiveRecord for Python Library" s.date = "2016-01-01 00:00:00" s.save()
删除:从表中删除模型对象
s = SampleModel.query(where=["title='py-activerecord'"], order=["id asc"]).first() s.delete()
许可证
本软件在麻省理工学院许可下发布
麻省理工学院执照(MIT) 版权所有(c)2016 Hiroyuki Nikaido
特此免费授予获得本软件副本和相关文档文件(以下简称“软件”)的任何人使用本软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或出售本软件副本的权利,并在符合以下条件的情况下,允许向其提供软件的人员这样做:
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
<软件>提供“原样”,无任何担保,明示或默示,包括但不限于适销性、适合特定用途和不侵权的保证。在任何情况下,无论是在合同、侵权行为或其他情况下,作者或著作权人都不应因软件、软件的使用或其他交易而产生或与之相关的索赔、损害或其他责任承担责任。