## Module 2: Run the Python Code

Run the Python code to connect to TiDB Cloud Starter cluster and do some basic operations.

### Step 1. Connect to TiDB Cloud Starter cluster

+ On the left **EXPLORER** of the VS Code, under folder `mysql_connector_python_demo`, click `demo_connection.py` file, you can briefly go through the source code. This Python script will connect to the lab TiDB Cloud Starter cluster and create a new database named `${LAB:SERVERLESS_CLUSTER_DATABASE_NAME}`.

+ Run `demo_connection.py` in the VS Code terminal.

  ```bash
  poetry run python mysql_connector_python_demo/demo_connection.py
  ```

  ```output
  Connected to gateway01.us-west-2.prod.aws.tidbcloud.com:4000 as 42yK4xxxxxxxxxxxxsP (Connection ID: 1233194)
  ...
  ```

  ![90%](https://public-pingcap-innovation.s3.us-west-2.amazonaws.com/demo-405/405-demo-connection-0307.jpg)

> [!IMPORTANT]
> The connection string is stored in the `.env` file.
> We will use the `get_connection` function in `demo_connection.py` to get a connection to the TiDB Cloud Starter Cluster for the following steps.

### Step 2. Insert data

+ On the left **EXPLORER** of the VS Code, under folder `mysql_connector_python_demo`, click `positional_placeholders.py` file, you can briefly go through the source code.

+ Insert data by positional placeholders by executing `positional_placeholders.py`.

  ```bash
  poetry run python mysql_connector_python_demo/positional_placeholders.py
  ```

  ```output
  1 row inserted into table t1 by positional placeholders
  ```

  ![90%](https://public-pingcap-innovation.s3.us-west-2.amazonaws.com/demo-405/405-positional-placeholders-0307.jpg)

+ On the left **EXPLORER** of the VS Code, under folder `mysql_connector_python_demo`, click `named_placeholders.py` file, you can briefly go through the source code.

+ Insert data by named placeholders.

  ```bash
  poetry run python mysql_connector_python_demo/named_placeholders.py
  ```

  ```output
  1 row inserted into table t1 by named placeholders
  ```

  ![90%](https://public-pingcap-innovation.s3.us-west-2.amazonaws.com/demo-405/405-named-placeholders-0307.jpg)

### Step 3. Query data

+ On the left **EXPLORER** of the VS Code, under folder `mysql_connector_python_demo`, click `query_fetch_all.py` file, you can briefly go through the source code.

+ Query data by `fetchall()` function.

  ```bash
  poetry run python mysql_connector_python_demo/query_fetch_all.py
  ```

  ```output
  PK of ABC is 1
  PK of DEF is 2
  ```

  ![90%](https://public-pingcap-innovation.s3.us-west-2.amazonaws.com/demo-405/405-fetchall-0307.jpg)

+ On the left **EXPLORER** of the VS Code, under folder `mysql_connector_python_demo`, click `query_fetch_one.py` file, you can briefly go through the source code.

+ Query data by `fetchone()` function.

  ```bash
  poetry run python mysql_connector_python_demo/query_fetch_one.py
  ```

  ```output
  PK of ABC is 1
  PK of DEF is 2
  ```

  ![90%](https://public-pingcap-innovation.s3.us-west-2.amazonaws.com/demo-405/405-fetchone-0307.jpg)
