Python Simple and small ORM PeeWee (dwarf) Introductory article

Source: Internet
Author: User

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
    • Pip
      pip install peewee

    • SOURCE Install

      git clone https://github.com/coleifer/peewee.gitcd peeweepython setup.py install
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.