The SQLAlchemy in Python

Source: Internet
Author: User

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

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.