Python in peewee module

Source: Internet
Author: User

Python in the PeeWee module, the need for friends can refer to the next.


Foreword about ORM Framework:

Brief introduction:

Object-Relational mapping (English: Objects Relational Mapping, abbreviated ORM, or O/RM, or O/R Mapping) is a program technology that enables the conversion of data between different types of systems in an object-oriented programming language. In effect, it actually creates a "virtual object database" that can be used in a programming language.

Object Relational Mapping (Object-relational Mapping) provides conceptual, easy-to-understand methods for modeling data. The ORM methodology is based on three core principles: simple: Modeling data in the most basic form. Communication: The database structure is documented in a language that anyone can understand. Accuracy: Create a properly standardized structure based on the data model. Typically, modelers develop information models by collecting information from people who are familiar with the application but are not proficient in data modelers. Modelers must be able to communicate with the data structure at the conceptual level using terminology that non-technical enterprise experts can understand. The modeler must also be able to process the sample data with simple unit analysis information. ORM is specifically designed to improve this connection.

ORM Advantages:

1. Hidden Data access details, "closed" Universal database interaction, ORM Core. He makes our universal database interaction simple and easy, without having to think about the damned SQL statements at all. Rapid development, resulting from this.

2. In the prehistoric era of the ORM chronology, we needed to transform our object model into a single SQL statement to construct our database system in a relational database via direct Connect or DB helper. Now, basically all ORM frameworks provide the ability to construct relational database structures through the object model.

PeeWee Module (ORM in Lightweight Python) 1. Install the PeeWee module:

(Ps: First install PIP, can execute the following command installation)
Linux:

sudo pip install PeeWee
Windows:cmd input below:

Pip Install PeeWee

2.peewee code example: preparing the database and table model:

#/usr/bin/python
# Encoding:utf-8
From PeeWee Import *from datetime
Import Date
# New Database
DBDB = sqlitedatabase (' people.db ')
#表格模型
Person: This is the concept of a model
Class Person (Model):
#CharField is an abstract data type equivalent to varchar
Name = Charfield ()
#DateField is equivalent to date
Birthday = Datefield ()
#BooleanField equivalent to BOOL
Is_relative = Booleanfield ()
# The database used is DB
Class Meta:database = db
#表格模型 Pet
Class Pet (Model):
#外连接的声明 (associated with person)
Owner = Foreignkeyfield (person, related_name= ' pets ')
Name = Charfield ()
Animal_type = Charfield ()
Class Meta:database = db
#连接数据库
Dbdb.connect ()
Build tables in db person and pet:
Db.create_tables ([Person, Pet])
Person and pet table data manipulation:
# Storing
Datauncle_bob = person (name= ' Bob ', Birthday=date (1967, 1,), is_relative=true)
Uncle_bob.save ()
Grandma = person.create (name= ' Grandma ', Birthday=date (1935, 3, 1), is_relative=true)
Herb = person.create (name= ' Herb ', Birthday=date (1950, 5, 1), Is_relative=false)
Grandma.name = ' Marry ' Grandma.save ()
Bob_kitty = Pet.create (owner=uncle_bob, name= ' Kitty ', animal_type= ' cat ')
Herb_fido = Pet.create (Owner=herb, name= ' Fido ', animal_type= ' dog ')
Herb_mittens = Pet.create (Owner=herb, name= ' mittens ', animal_type= ' cat ')
Herb_mittens_jr = Pet.create (Owner=herb, Name= ' Mittens Jr ', animal_type= ' cat ')
# return the value of Delete_instance () is the number of rows removed form the database
# Delete
Dataherb_mittens.delete_instance ()
# He had a great life
# Modify
Dataherb_fido.owner = Uncle_bobherb_fido.save ()
Bob_fido = Herb_fido
# Rename our variable for clarityperson,pet->select operation:
# Retrieving Data
# query person whose name is marry
Grandma = Person.select (). WHERE (Person.name = = ' Marry '). Get ()
#列出Person表中所有的person
For WHO in Person.select ():
Print Person.name, person.is_relative
#查询Pet表中animal_type为cat的所有pet
Query = (pet. Select (PET). Join (person). WHERE (Pet.animal_type = = ' Cat '))
For pet in query:
Print Pet.name, Pet.owner.name
#查询Pet表中主人名为Bob的所有pet
For pet in Pet.select (). Join (person). WHERE (Person.name = = ' Bob '):
Print Pet.name
#查询Pet表中person为uncle_bob的所有pet
For pet in Pet.select (). WHERE (Pet.owner = = Uncle_bob):
Print Pet.name
#查询Pet表中person为uncle_bob结果按pet名排列
For pet in Pet.select (). WHERE (Pet.owner = = Uncle_bob). Order_by (Pet.name):
Print Pet.name #将Person表中的person按生日降序查询
For WHO in Person.select (). Order_by (Person.birthday.desc ()):
Print Person.name, Person.birthday
#查询Person表中person所拥有的pet数量及名字和类型
For WHO in Person.select ():
Print Person.name, Person.pets.count (), ' pets '
For pet in Person.pets:print ', Pet.name, Pet.animal_type
#查询Person表中生日小于1940或大于1960的person
d1940 = Date (1940, 1, 1)
d1960 = Date (1960, 1, 1)
query = (person. Select (). where ((Person.birthday < d1940) | (Person.birthday > d1960)))
#查询Person表中生日在1940和1960之间的person
For WHO in query:
Print Person.name, Person.birthday
query = (person. Select (). where ((Person.birthday > d1940) & (Person.birthday < d1960)))
For WHO in query:
Print Person.name, Person.birthday
Person in #按照expression查询person名开头为小写或大写 G
Expression = (fn. Lower (FN. Substr (Person.name, 1, 1)) = = ' G ')
In Person.select (). where (expression):
Print Person.nameperson, pet->update
Operation q = user.update (Active=false). WHERE (user.registration_expired = = True) q.execute () person, Pet->insert
Operation q = User.insert (username= ' admin ', active=true, Registration_expired=false) q.execute () person, Pet->delete
Operation q = User.delete (). WHERE (user.active = = False) Q.execute ()
Close Database Db.close ()
Summarize

There are a lot of specific things about PeeWee, I just listed a few basic operations, in the development process need to know more content, please
Refer to PeeWee official API, see: PEEWEEAPI

Python in peewee module

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.