Flask ==> Orm sqlalchemy1. What is ORM
ORM, the Object-relational Mapping (Object relational mapping), is a mapping between a relational database and a business entity object,
In this way, we do not need to deal with complex SQL statements when we operate the business objects, simply manipulate the object's properties and methods.
What is 2.SQLALchemy?
SQLAlchemy is an ORM framework based on Python implementations.
The framework builds on the DB API and uses relational object mappings for database operations.
Simply put: Convert classes and objects to SQL, then execute SQL using the Data API and get execution results.
Installation:
PIP3 Install SQLAlchemy
Part:
- Engine, Frame engines
- Connection Pooling, database connection pool
- Dialect, select the type of DB API to connect to the database
- Schema/types, architecture and type
- SQL exprression language,sql Expression language
SQLAlchemy itself cannot operate the database, it must have been pymsql and other third-party plugins.
Dialect is used to communicate with the data API, invoking different database APIs depending on the configuration file, enabling operations on the database, such as:
mysql-Python MySQL+mysqldb://<user>:<password>@pymysql MySQL+pymysql://<username>:<password>@] MySQL-Connector MySQL+mysqlconnector://<user>:<password>@ cx_oracle Oracle+cx_oracle://user:pass@host:p ort/dbname[?key=value& key=Value ...] MORE: http://docs.sqlalchemy.org/en/latest/dialects/index.html
Q: What is the difference between SQLALchemy and Django?
SQLAlchemy (ORM): It is separate, all places are available, closer to SQL, not automatically generate a third table, you can not connect the database, you need to connect to the database through a third-party module
Django: It is only used in Django itself, it is easier to do cross-table, can automatically generate a third table, you do not have a database, you need to connect to the database through a third-party module.
3.SQLALchemy use 1. Execute native SQL statements
Import TimeImportThreadingImportSQLAlchemy fromSQLAlchemyImportCreate_engine fromSqlalchemy.engine.baseImportEngine engine=Create_engine ("Mysql+pymysql://root:[email Protected]:3306/t1?charset=utf8", Max_overflow=0,#most connections created outside the connection pool sizePool_size=5,#Connection Pool SizePool_timeout=30,#There are no threads in the pool waiting for the most time, otherwise the errorPool_recycle=-1#how long after a connection to a thread in a thread pool is recycled (reset)) defTask (ARG): Conn=engine.raw_connection () cursor=conn.cursor () cursor.execute ("SELECT * from T1") Result=Cursor.fetchall () cursor.close () Conn.close ( ) forIinchRange (20): T= Threading. Thread (Target=task, args=(i,)) T.start ()
The SQLAlchemy of Flask ==> ORM