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