Python Learning Note eight: ORM Framework SQLAlchemy

Source: Internet
Author: User

One: SQLAlchemy use

1: Creation of entity classes

The entity classes in ORM are different from the generic Python class, where __tablename__= "" is used to indicate that the class corresponds to a table in the database, and then defines a series of member properties that use Column (data type) To map to a specific column in the table.

First, create the database engine and get a base class from a static method: Declarative_base () creates a Basemodel class that can be automatically associated with a table subclass.

Then, inherit the base class and define the entity class:

Entity classes with foreign keys are created:

Finally, run the method that comes with the base class, pass the database engine in, create the database table: BaseModel.metadata.create_all will find all the subclasses of Basemodel and build them in the database

2: Create a record

Creates a class instance that corresponds to a database record.

3: Create a database session

4: Manipulating the database using an object-oriented approach

Increase:

Add a record:

Add multiple records:

Check:

Conditional query:

Full Table query:

Sort query:

Filter query (In/not in):

Statistics Query record number:

Logical query: and/or followed by left and right operands.

Join query:

Other things to do: Refer to this blog post:

http://blog.csdn.net/fengzhongzhishenfu/article/details/38978863

5: Commit the transaction for the persistence operation to take effect

Python Learning Note eight: ORM Framework SQLAlchemy

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.