Ponyorm: detailed explanation of the ORM framework of the base in Python
Ponyorm: detailed explanation of the ORM framework of the base in Python
Introduction:
ORM (Object-Relational Mapping) is a technology. It maps the object-oriented language program object model and the data model of the relationship database to realize the interoperability between object-oriented programming language and database.Ponyorm is a Python -based ORM framework that provides a simple and powerful way to handle database operations.
This article will introduce the usage and related configuration of Ponyorm in detail to help readers understand how the framework provides data persistence and provides example code to help readers better understand and apply this framework.
1. Install ponyorm
First, we need to install ponyorm.You can install ponyorm through the following command:
pip install pony
2. Create a database connection configuration
Before using Ponyorm, we need to configure the database connection.Suppose we are using the MySQL database, which can be added to the code:
python
from pony import orm
db = orm.Database()
db.bind(provider='mysql', host='localhost', user='username', passwd='password', db='database_name')
The above code creates a database connection object and is bound to the MySQL database.You need to fill in the host name (host), username (User), password (PASSWD), and database name (DB).
Third, define the physical class
In Ponyorm, we need to define the physical class to map the table in the database.The physical class can be defined as the Python class, and each class corresponds to one table.
python
class User(db.Entity):
id = orm.PrimaryKey(int, auto=True)
name = orm.Required(str)
# Add relationship attribute
products = orm.Set("Product")
class Product(db.Entity):
id = orm.PrimaryKey(int, auto=True)
name = orm.Required(str)
user = orm.Required(User)
The above sample code defines two physical classes User and Product, which corresponds to the user tables and product tables in the database.The fields in the physical class correspond to the columns in the database table. Different modifiers are used to specify the attributes of the field. For example, Primarykey represents the primary key, and Required represents the necessary field.At the same time, we can also establish a connection between physical classes by defining the attributes of the relationship.
4. Create a database table
After we define the physical class, we need to map the physical class and the database table through the `db.Gene on_mapping ()` to create the corresponding data table.The following is an example code:
python
db.generate_mapping(create_tables=True)
5. Database operation
With Ponyorm, we can perform various database operations, such as inserting, querying, updating, and deleting data.
1. Insert data
python
with orm.db_session:
user = User(name='John Doe')
product = Product(name='Apple', user=user)
orm.commit()
The above code example creates a User instance and an product instance, and inserts them into the database.Use `Oreommit ()` to submit transactions.
2. Query data
python
with orm.db_session:
users = orm.select(u for u in User if u.name.startswith('John')).order_by(User.id)[:]
print(users)
The above example code query all the records starting with 'John' in the User table and sorted according to the ID.
3. Update data
python
with orm.db_session:
user = orm.select(u for u in User if u.id == 1).first()
user.name = 'Jane Doe'
orm.commit()
The above code example updates the name of the ID in the USER table as 1 to 'Jane Doe'.
4. Delete data
python
with orm.db_session:
user = orm.select(u for u in User if u.id == 1).first()
orm.delete(user)
orm.commit()
The above code example deletes the record of the ID in the USER table as 1.
The above is the basic use of Ponyorm and related configurations.By using Ponyorm, we can more conveniently perform database operations and more efficiently implement the interactive operation between the Python program and the database.