Connect to TiDB with SQLAlchemy

TiDB is a MySQL-compatible database, and SQLAlchemy is a popular Python SQL toolkit and Object Relational Mapper (ORM).

In this tutorial, you can learn how to use TiDB and SQLAlchemy to accomplish the following tasks:

  • Set up your environment.
  • Connect to your TiDB cluster using SQLAlchemy.
  • Build and run your application. Optionally, you can find sample code snippets for basic CRUD operations.

Prerequisites

To complete this tutorial, you need:

If you don't have a TiDB cluster, you can create one as follows:

Run the sample app to connect to TiDB

This section demonstrates how to run the sample application code and connect to TiDB.

Step 1: Clone the sample app repository

Run the following commands in your terminal window to clone the sample code repository:

git clone https://github.com/tidb-samples/tidb-python-sqlalchemy-quickstart.git cd tidb-python-sqlalchemy-quickstart

Step 2: Install dependencies

Run the following command to install the required packages (including SQLAlchemy and PyMySQL) for the sample app:

pip install -r requirements.txt

Why use PyMySQL?

SQLAlchemy is an ORM library that works with multiple databases. It provides a high-level abstraction of the database, which helps developers write SQL statements in a more object-oriented way. However, SQLAlchemy does not include a database driver. To connect to a database, you need to install a database driver. This sample application uses PyMySQL as the database driver, which is a pure Python MySQL client library that is compatible with TiDB and can be installed on all platforms.

You can also use other database drivers, such as mysqlclient and mysql-connector-python. But they are not pure Python libraries and require the corresponding C/C++ compiler and MySQL client for compiling. For more information, refer to SQLAlchemy official documentation.

Step 3: Configure connection information

Connect to your TiDB cluster depending on the TiDB deployment option you've selected.

  • TiDB Serverless
  • TiDB Dedicated
  • TiDB Self-Hosted
  1. Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.

  2. Click Connect in the upper-right corner. A connection dialog is displayed.

  3. Ensure the configurations in the connection dialog match your operating environment.

    • Endpoint Type is set to Public
    • Branch is set to main
    • Connect With is set to General
    • Operating System matches your environment.
  4. Click Generate Password to create a random password.

  5. Run the following command to copy .env.example and rename it to .env:

    cp .env.example .env
  6. Copy and paste the corresponding connection string into the .env file. The example result is as follows:

    TIDB_HOST='{host}' # e.g. gateway01.ap-northeast-1.prod.aws.tidbcloud.com TIDB_PORT='4000' TIDB_USER='{user}' # e.g. xxxxxx.root TIDB_PASSWORD='{password}' TIDB_DB_NAME='test' CA_PATH='{ssl_ca}' # e.g. /etc/ssl/certs/ca-certificates.crt (Debian / Ubuntu / Arch)

    Be sure to replace the placeholders {} with the connection parameters obtained from the connection dialog.

  7. Save the .env file.

  1. Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.

  2. Click Connect in the upper-right corner. A connection dialog is displayed.

  3. Click Allow Access from Anywhere and then click Download CA cert to download the CA certificate.

    For more details about how to obtain the connection string, refer to TiDB Dedicated standard connection.

  4. Run the following command to copy .env.example and rename it to .env:

    cp .env.example .env
  5. Copy and paste the corresponding connection string into the .env file. The example result is as follows:

    TIDB_HOST='{host}' # e.g. tidb.xxxx.clusters.tidb-cloud.com TIDB_PORT='4000' TIDB_USER='{user}' # e.g. root TIDB_PASSWORD='{password}' TIDB_DB_NAME='test' CA_PATH='{your-downloaded-ca-path}'

    Be sure to replace the placeholders {} with the connection parameters obtained from the connection dialog, and configure CA_PATH with the certificate path downloaded in the previous step.

  6. Save the .env file.

  1. Run the following command to copy .env.example and rename it to .env:

    cp .env.example .env
  2. Copy and paste the corresponding connection string into the .env file. The example result is as follows:

    TIDB_HOST='{tidb_server_host}' TIDB_PORT='4000' TIDB_USER='root' TIDB_PASSWORD='{password}' TIDB_DB_NAME='test'

    Be sure to replace the placeholders {} with the connection parameters, and remove the CA_PATH line. If you are running TiDB locally, the default host address is 127.0.0.1, and the password is empty.

  3. Save the .env file.

Step 4: Run the code and check the result

  1. Execute the following command to run the sample code:

    python sqlalchemy_example.py
  2. Check the Expected-Output.txt to see if the output matches.

Sample code snippets

You can refer to the following sample code snippets to complete your own application development.

For complete sample code and how to run it, check out the tidb-samples/tidb-python-sqlalchemy-quickstart repository.

Connect to TiDB

from sqlalchemy import create_engine, URL from sqlalchemy.orm import sessionmaker def get_db_engine(): connect_args = {} if ${ca_path}: connect_args = { "ssl_verify_cert": True, "ssl_verify_identity": True, "ssl_ca": ${ca_path}, } return create_engine( URL.create( drivername="mysql+pymysql", username=${tidb_user}, password=${tidb_password}, host=${tidb_host}, port=${tidb_port}, database=${tidb_db_name}, ), connect_args=connect_args, ) engine = get_db_engine() Session = sessionmaker(bind=engine)

When using this function, you need to replace ${tidb_host}, ${tidb_port}, ${tidb_user}, ${tidb_password}, ${tidb_db_name} and ${ca_path} with the actual values of your TiDB cluster.

Define a table

from sqlalchemy import Column, Integer, String from sqlalchemy.orm import declarative_base Base = declarative_base() class Player(Base): id = Column(Integer, primary_key=True) name = Column(String(32), unique=True) coins = Column(Integer) goods = Column(Integer) __tablename__ = "players"

For more information, refer to SQLAlchemy documentation: Mapping classes with declarative.

Insert data

with Session() as session: player = Player(name="test", coins=100, goods=100) session.add(player) session.commit()

For more information, refer to Insert data.

Query data

with Session() as session: player = session.query(Player).filter_by(name == "test").one() print(player)

For more information, refer to Query data.

Update data

with Session() as session: player = session.query(Player).filter_by(name == "test").one() player.coins = 200 session.commit()

For more information, refer to Update data.

Delete data

with Session() as session: player = session.query(Player).filter_by(name == "test").one() session.delete(player) session.commit()

For more information, refer to Delete data.

Next steps

Need help?

Ask questions on the Discord, or create a support ticket.

Was this page helpful?