In Python, you can use SQLAlchemy to manipulate a database.
SQLAlchemy is a standard library in Python.
To use SQLAlchemy, you first create a connection :
url = mysql+pymysql://root:[email PROTECTED]/DBNAME?CHARSET=UTF8MB4
engine = create_engine (URL)
Note The URL here, wherepymysql is a library used in python3.x to connect to the MySQL server
Using mysqldb in Python2
Create a database engine with create_engine
A session is a context , which is located in the server-side memory , from the beginning of communication to the end of the communication.
The session Records information about the connection to the database, such as user information, whether the changes are automatically submitted, and so on.
then establish the session:
Session = Sessionmaker (bind = engine, autocommit = False)
Session = Session ()
Sessionmaker generates a database session class , an instance of this class that can be used to manipulate the database .
To make the Session object unique for each thread in multiple threads, you can use scoped_session:
Dbsession = Scoped_session (session)
Dbsession = Dbsession ()
You can then use dbsession to manipulate the database, such as:
Dbsession.commit (), Dbsession.rollback () and so on.
The SQLAlchemy in Python