Skip to content

Commit

Permalink
update dev-guide-sample-application-python-mysqlclient (#16075) (#16312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jan 24, 2024
1 parent bb83ed6 commit a45da19
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions develop/dev-guide-sample-application-python-mysqlclient.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TiDB 是一个兼容 MySQL 的数据库。[mysqlclient](https://github.com/PyMyS
## 前置需求

- 推荐 [Python **3.10**](https://www.python.org/downloads/) 及以上版本。
- 推荐 [Python 3.8](https://www.python.org/downloads/) 及以上版本。
- [Git](https://git-scm.com/downloads)
- TiDB 集群。如果你还没有 TiDB 集群,可以按照以下方式创建:
- (推荐方式)参考[创建 TiDB Serverless 集群](/develop/dev-guide-build-cluster-in-cloud.md#第-1-步创建-tidb-serverless-集群),创建你自己的 TiDB Cloud 集群。
Expand Down Expand Up @@ -86,7 +86,7 @@ pip install -r requirements.txt
6. 复制并粘贴对应连接字符串至 `.env` 中。示例结果如下:

```dotenv
TIDB_HOST='{host}' # e.g. gateway01.ap-northeast-1.prod.aws.tidbcloud.com
TIDB_HOST='{host}' # e.g. xxxxxx.aws.tidbcloud.com
TIDB_PORT='4000'
TIDB_USER='{user}' # e.g. xxxxxx.root
TIDB_PASSWORD='{password}'
Expand Down Expand Up @@ -121,9 +121,9 @@ pip install -r requirements.txt
5. 复制并粘贴对应的连接字符串至 `.env` 中。示例结果如下:

```dotenv
TIDB_HOST='{host}' # e.g. tidb.xxxx.clusters.tidb-cloud.com
TIDB_HOST='{host}' # e.g. xxxxxx.aws.tidbcloud.com
TIDB_PORT='4000'
TIDB_USER='{user}' # e.g. root
TIDB_USER='{user}' # e.g. xxxxxx.root
TIDB_PASSWORD='{password}'
TIDB_DB_NAME='test'
CA_PATH='{your-downloaded-ca-path}'
Expand Down Expand Up @@ -182,29 +182,29 @@ pip install -r requirements.txt
```python
def get_mysqlclient_connection(autocommit:bool=True) -> MySQLdb.Connection:
db_conf = {
"host": ${tidb_host},
"port": ${tidb_port},
"user": ${tidb_user},
"password": ${tidb_password},
"database": ${tidb_db_name},
"host": '${tidb_host}',
"port": '${tidb_port}',
"user": '${tidb_user}',
"password": '${tidb_password}',
"database": '${tidb_db_name}',
"autocommit": autocommit
}
if ${ca_path}:
if '${ca_path}':
db_conf["ssl_mode"] = "VERIFY_IDENTITY"
db_conf["ssl"] = {"ca": ${ca_path}}
db_conf["ssl"] = {"ca": '${ca_path}'}
return MySQLdb.connect(**db_conf)
```
在使用该函数时,你需要将 `${tidb_host}``${tidb_port}``${tidb_user}``${tidb_password}``${tidb_db_name}` 等替换为你的 TiDB 集群的实际值。
在使用该函数时,你需要将 `${tidb_host}``${tidb_port}``${tidb_user}``${tidb_password}``${tidb_db_name}``${ca_path}` 等替换为你的 TiDB 集群的实际值。
### 插入数据
```python
with get_mysqlclient_connection(autocommit=True) as conn:
with conn.cursor() as cur:
player = ("1", 1, 1)
player = ("test", 1, 1)
cursor.execute("INSERT INTO players (id, coins, goods) VALUES (%s, %s, %s)", player)
```
Expand All @@ -226,8 +226,8 @@ with get_mysqlclient_connection(autocommit=True) as conn:
```python
with get_mysqlclient_connection(autocommit=True) as conn:
with conn.cursor() as cur:
player_id, amount, price="1", 10, 500
cursor.execute(
player_id, amount, price="test", 10, 500
cur.execute(
"UPDATE players SET goods = goods + %s, coins = coins + %s WHERE id = %s",
(-amount, price, player_id),
)
Expand All @@ -240,8 +240,8 @@ with get_mysqlclient_connection(autocommit=True) as conn:
```python
with get_mysqlclient_connection(autocommit=True) as conn:
with conn.cursor() as cur:
player_id = "1"
cursor.execute("DELETE FROM players WHERE id = %s", (player_id,))
player_id = "test"
cur.execute("DELETE FROM players WHERE id = %s", (player_id,))
```
更多信息参考[删除数据](/develop/dev-guide-delete-data.md)。
Expand All @@ -256,7 +256,7 @@ Python 驱动程序提供对数据库的底层访问,但要求开发者:
- 手动管理数据库事务
- 手动将数据行(在 mysqlclient 中表示为元组 (tuple))映射为数据对象
建议仅在需要编写复杂的 SQL 语句时使用驱动程序。其他情况下,建议使用 [ORM](https://zh.wikipedia.org/wiki/对象关系映射) 框架进行开发,例如 [SQLAlchemy](/develop/dev-guide-sample-application-python-sqlalchemy.md)、[Peewee](/develop/dev-guide-sample-application-python-peewee.md) 和 Django。ORM 可以帮助你:
建议仅在需要编写复杂的 SQL 语句时使用驱动程序。其他情况下,建议使用 [ORM](https://zh.wikipedia.org/wiki/对象关系映射) 框架进行开发,例如 [SQLAlchemy](/develop/dev-guide-sample-application-python-sqlalchemy.md)、[Peewee](/develop/dev-guide-sample-application-python-peewee.md) 和 [Django](/develop/dev-guide-sample-application-python-django.md)。ORM 可以帮助你:
- 减少管理连接和事务的[模板代码](https://en.wikipedia.org/wiki/Boilerplate_code)
- 使用数据对象代替大量 SQL 语句来操作数据
Expand Down

0 comments on commit a45da19

Please sign in to comment.