Python orm is diverse, such as: Django Orm, SQLAlchemy, Sqlobject, today we introduce a compact and lean framework, supported databases: SQLITE,MYSQL,POSTGRESQL, supported Python version: 2.6 + and 3.2+
Official documentation Links
1. Installing PeeWee
2. Hello worldhello_world.py
from peewee import *DB = SqliteDatabase(‘helloworld.db‘)class BaseModle(Modle): """ 基础模型 """ class Meta(): database = DBclass HelloWorld(BaseModle): """ hello world模型 peewee模型及数据库表,表及模型 """ hwid = PrimaryKeyField(unique=True) hwcontent = CharField() @classmethod def save_info(cls, hw_content): """ 保存数据 "" HelloWorld(hwcontent=hw_content).save() @classmethod def select_info(cls, hw_content=None): """ 查询数据 """ if hw_content: cls.select().where(HelloWorld.hwcontent == hw_content) else: cls.select()
test_hello_world.py
from hello_world import DB, HelloWorlddef db_test(): DB.create_tables([HelloWorld], safe=True) HelloWorld.save_info(‘hello world my peewee.‘)if __name__ == ‘__main__‘: db_test() for hw in HelloWorld.select_info(): print(hw.hwcontent)
Done
Focus on understanding PeeWee vs Database Relational tables
Thing |
corresponds to ... |
Model |
Database table |
Field instance |
Column on a table |
Model instance |
Row in a database table |
Python Simple and small ORM PeeWee (dwarf) Introductory article