SQLAlchemy is powerful, well documented, and a heavyweight ORM framework. Peewee is a lightweight ORM framework that supports Python 2.7+ and 3.4+, SQLite, MySQL, and PostgreSQL. If you are familiar with Djangos ORM, Peewee will be very cheap to learn.

The installation

pip install peeweeCopy the code

The model definition

from peewee import *
​
db = SqliteDatabase('people.db')
​
class BaseModel(Model):
    class Meta:
        database = db
​
class Person(BaseModel):
    name = CharField(verbose_name='name', max_length=10, null=False, index=True)
    gender = IntegerField(verbose_name='last name don't', null=False, default=1)
    birthday = DateField(verbose_name='birthday', null=True, default=None)
​
    class Meta:
        table_name = 'people'Copy the code

First, we define our model class Person (those of you who have used Django will be familiar with this model definition, which is very similar to the Django model definition) and use SqliteDatabase to specify the database people.db to use.

Then we define the data fields of the table Person. If we do not specify the primary key, Peewee will automatically create an ID field for us as the primary key. Each Field has several parameters that can be configured, the size of the length, whether it is null and default, index and unique, and several common database options.

Create a database table

Person.create_table()
# or
db.create_tables([Person])Copy the code

Operating database

  • increase

    Create the instance directly, then call the instance method save().

    Instances can also be created and saved using the create() class method.

    p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
    p.save()
    ​
    jerry = Person.create(name='jerry', gender=0, birthday=date(1999, 12, 1))Copy the code
  • delete

    Run delete().where().execute() to delete conditions. Where () is the delete condition, and execute() to delete conditions.

    If the instance object is already queried, the instance method delete_instance() is called to delete it.

    Select * from Tom where name = 'Tom
    Person.delete().where(Person.name=='tom').execute()
    ​
    # call delete_instance() to delete an instantiated object
    p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
    p.save()
    p.delete_instance()Copy the code
  • change

    Use update().wahere().excute() for conditional updates. For the data objects that have been queried, after modifying the object attributes, directly save() for update.

    Select * from 'save' where id = 'primary key'
    p = Person(name='tom', gender=1, birthday=date(2000, 1, 1))
    p.save()
    p.gender = 0
    p.save()
    ​
    # update Jerry's birthday data
    q = Person.update({Person.birthday: date(1999, 12, 12)}).where(Person.name=='jerry')
    q.execute()Copy the code
  • check

    Individual data queries use person.get (), or person.select ().where().get().

    Person.select().where()

    # select * from single data
    p = Person.select().where(Person.name=='tom').get()
    print(p.name, p.gender, p.birthday)
    ​
    # use the shorthand model.get ()
    p = Person.get(Person.name=='tom')
    print(p.name, p.gender, p.birthday)
    ​
    # query multiple data sets
    people = Person.select().where(Person.gender==1)
    for p in people:
        print(p.name, p.gender, p.birthday)Copy the code