Article From:https://www.cnblogs.com/allen2333/p/9059997.html

1. concept”

  1. Target: class / object operation -> SQL statement -> DB API -> and then executed in database. ORM does the first two parts, because ORM itself cannot operate the database. Reference: https://baike.baidu.com/Item/ORM/3583252? Fr=aladdin
  2. Essence: class (Users) corresponds to the database table, and the attributes in the class correspond to the columns in the database table.
class Users(Base):
    __tablename__ = 'users' # Database table nameId = Column (Integer, primary_key=True) primary key of IDName = Column (String (32), index=True,Nullable=False) name column,

2. SQLAlchemy

2.1 SQLAlch.”Emy is a ORM framework based on Python implementation. The framework is built on DB API, using relational object mapping for database operations. In short, the class and object are converted to SQL, then the database API is used to execute SQL and the execution results are obtained.

Engine,The engine of the frame
Connection Pooling ,Database connection pool
Dialect,Select the type of DB API that connects the database
Schema/Types,Architecture and type
SQL Exprression Language,SQLExpression language

2.2 SQLAlchemy itself is inoperable.”According to the library, it must be pymsql and other three party plug-ins, Dialect is used to communicate with data API, and the different database API is called according to the different configuration files, so as to realize the operation of the database, such as:

This heap string is in Dialect

MySQL-Python
    mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
    
pymysql
    mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
    
MySQL-Connector
    mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
    
cx_Oracle
    oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
    
More: http://docs.sqlalchemy.org/en/latest/dialects/index.html
Link of this Article: Python – ORM (database related)

Leave a Reply

Your email address will not be published. Required fields are marked *