Connect to TiDB with peewee
TiDB is a MySQL-compatible database, and peewee is a popular Object Relational Mapper (ORM) for Python.
In this tutorial, you can learn how to use TiDB and peewee to accomplish the following tasks:
- Set up your environment.
- Connect to your TiDB cluster using peewee.
- Build and run your application. Optionally, you can find sample code snippets for basic CRUD operations.
Prerequisites
To complete this tutorial, you need:
- Python 3.8 or higher.
- Git.
- A TiDB cluster.
If you don't have a TiDB cluster, you can create one as follows:
- (Recommended) Follow Creating a TiDB Serverless cluster to create your own TiDB Cloud cluster.
- Follow Deploy a local test TiDB cluster or Deploy a production TiDB cluster to create a local cluster.
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-peewee-quickstart.git
cd tidb-python-peewee-quickstart
Step 2: Install dependencies
Run the following command to install the required packages (including peewee and PyMySQL) for the sample app:
pip install -r requirements.txt
Why use PyMySQL?
peewee 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, peewee 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. For more information, refer to peewee 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
Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
Click Connect in the upper-right corner. A connection dialog is displayed.
Ensure the configurations in the connection dialog match your operating environment.
- Connection Type is set to
Public
- Branch is set to
main
- Connect With is set to
General
- Operating System matches your environment.
- Connection Type is set to
Click Generate Password to create a random password.
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .envCopy 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.Save the
.env
file.
Navigate to the Clusters page, and then click the name of your target cluster to go to its overview page.
Click Connect in the upper-right corner. A connection dialog is displayed.
In the connection dialog, select Public from the Connection Type drop-down list, and then click CA cert to download the CA certificate.
If you have not configured the IP access list, click Configure IP Access List or follow the steps in Configure an IP Access List to configure it before your first connection.
In addition to the Public connection type, TiDB Dedicated supports Private Endpoint and VPC Peering connection types. For more information, see Connect to Your TiDB Dedicated Cluster.
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .envCopy 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 configureCA_PATH
with the certificate path downloaded in the previous step.Save the
.env
file.
Run the following command to copy
.env.example
and rename it to.env
:cp .env.example .envCopy 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 theCA_PATH
line. If you are running TiDB locally, the default host address is127.0.0.1
, and the password is empty.Save the
.env
file.
Step 4: Run the code and check the result
Execute the following command to run the sample code:
python peewee_example.pyCheck 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-peewee-quickstart repository.
Connect to TiDB
from peewee import MySQLDatabase
def get_db_engine():
config = Config()
connect_params = {}
if ${ca_path}:
connect_params = {
"ssl_verify_cert": True,
"ssl_verify_identity": True,
"ssl_ca": ${ca_path},
}
return MySQLDatabase(
${tidb_db_name},
host=${tidb_host},
port=${tidb_port},
user=${tidb_user},
password=${tidb_password},
**connect_params,
)
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 peewee import Model, CharField, IntegerField
db = get_db_engine()
class BaseModel(Model):
class Meta:
database = db
class Player(BaseModel):
name = CharField(max_length=32, unique=True)
coins = IntegerField(default=0)
goods = IntegerField(default=0)
class Meta:
table_name = "players"
For more information, refer to peewee documentation: Models and Fields.
Insert data
# Insert a single record
Player.create(name="test", coins=100, goods=100)
# Insert multiple records
Player.insert_many(
[
{"name": "test1", "coins": 100, "goods": 100},
{"name": "test2", "coins": 100, "goods": 100},
]
).execute()
For more information, refer to Insert data.
Query data
# Query all records
players = Player.select()
# Query a single record
player = Player.get(Player.name == "test")
# Query multiple records
players = Player.select().where(Player.coins == 100)
For more information, refer to Query data.
Update data
# Update a single record
player = Player.get(Player.name == "test")
player.coins = 200
player.save()
# Update multiple records
Player.update(coins=200).where(Player.coins == 100).execute()
For more information, refer to Update data.
Delete data
# Delete a single record
player = Player.get(Player.name == "test")
player.delete_instance()
# Delete multiple records
Player.delete().where(Player.coins == 100).execute()
For more information, refer to Delete data.
Next steps
- Learn more usage of peewee from the documentation of peewee.
- Learn the best practices for TiDB application development with the chapters in the Developer guide, such as Insert data, Update data, Delete data, Single table reading, Transactions, and SQL performance optimization.
- Learn through the professional TiDB developer courses and earn TiDB certifications after passing the exam.
Need help?
Ask questions on TiDB Community, or create a support ticket.