Define Model

When using, first define the Model according to the requirements, and then create a table through create_tables(). If the database table has been created, you can directly create the Model through the python -m pwiz script tool

new data table

Define the Model first, and then create a table through db.create_tables() or Model.create_table().

For example, we need to create a Person table with three fields name, birthday and is_relative. The Model we define is as follows:

from peewee import *

# Connect to the database
database = MySQLDatabase('test', user='root', host='localhost', port=3306)

# define Person
class Person(Model):
     name = CharField()
     birthday = DateField()
     is_relative = BooleanField()

     classMeta:
         database = database

Then, we can create the table

# create table
Person. create_table()

# Create a table can also be like this, you can create multiple
# database. create_tables([Person])

Among them, CharField, DateField, BooleanField and other types correspond to the data types in the database one by one, we can use it directly, as for the conversion of CharField => varchar(255), Peewee has already done it for us

Existing data table

If the database already exists, create models in batches directly through python -m pwiz.
For example, above I have created the test library, and created the Person table, which has id, name, birthday and is_relative fields. Then, I can use the following command:

# Specify mysql, the user is root, the host is localhost, and the database is test
python -m pwiz -e mysql -H localhost -p 3306 -u root -P root -t elephdev_table elephdev_db > dbmodel/elephdev_table.py

Operate the database

insert

Create an example directly, and then use save() to add a new piece of data

# add a piece of data
p = Person(name='liuchungui', birthday=date(1990, 12, 20), is_relative=True)
p. save()

delete

Use delete().where().execute() to delete, where() is the condition, and execute() is responsible for executing the statement. If the instance has been queried, delete it directly with delete_instance()

# Delete the data named perter
Person.delete().where(Person.name == 'perter').execute()

# already instantiated data, use delete_instance
p = Person(name='liuchungui', birthday=date(1990, 12, 20), is_relative=False)
p.id = 1
p. save()
p. delete_instance()

update

If yes, the instance that has already added data or the queried data instance, and the table has a primary key, use save() at this time to modify the data; if there is no instance, use update().where() to update the data

# The data that has been instantiated, and the primary key of id is specified, then saving at this time is updating the data
p = Person(name='liuchungui', birthday=date(1990, 12, 20), is_relative=False)
p.id = 1
p. save()

# Update birthday data
q = Person.update({Person.birthday: date(1983, 12, 21)}).where(Person.name == 'liuchungui')
q. execute()

select

Use Person.get() for a single piece of data, or use Person.select().where().get(). If you want to query multiple pieces of data, use Person.select().where() and remove get(). The syntax is very intuitive, select() is the query, where is the condition, get is to get the first piece of data

# Query a single piece of data
p = Person.get(Person.name == 'liuchungui')
print(p.name, p.birthday, p.is_relative)

# Use where().get() query
p = Person.select().where(Person.name == 'liuchungui').get()
print(p.name, p.birthday, p.is_relative)

# Query multiple data
persons = Person. select(). where(Person. is_relative == True)
for p in persons:
     print(p.name, p.birthday, p.is_relative)
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部