Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#11742
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
Icemap authored and ti-chi-bot committed Mar 23, 2023
1 parent 0967b20 commit 13cdc87
Show file tree
Hide file tree
Showing 10 changed files with 1,778 additions and 458 deletions.
5 changes: 3 additions & 2 deletions TOC-tidb-cloud.md
Expand Up @@ -18,9 +18,10 @@
- [Build a TiDB Cluster in TiDB Cloud (Serverless Tier)](/develop/dev-guide-build-cluster-in-cloud.md)
- [CRUD SQL in TiDB](/develop/dev-guide-tidb-crud-sql.md)
- Example Applications
- [Java](/develop/dev-guide-sample-application-java.md)
- [Java (Spring Boot)](/develop/dev-guide-sample-application-spring-boot.md)
- [Golang](/develop/dev-guide-sample-application-golang.md)
- [Java (Spring Boot)](/develop/dev-guide-sample-application-spring-boot.md)
- [Java](/develop/dev-guide-sample-application-java.md)
- [Python](/develop/dev-guide-sample-application-python.md)
- Connect to TiDB
- [Choose Driver or ORM](/develop/dev-guide-choose-driver-or-orm.md)
- [Connect to TiDB](/develop/dev-guide-connect-to-tidb.md)
Expand Down
7 changes: 3 additions & 4 deletions TOC.md
Expand Up @@ -22,9 +22,10 @@
- [Build a TiDB Cluster in TiDB Cloud (Serverless Tier)](/develop/dev-guide-build-cluster-in-cloud.md)
- [CRUD SQL in TiDB](/develop/dev-guide-tidb-crud-sql.md)
- Example Applications
- [Java](/develop/dev-guide-sample-application-java.md)
- [Java (Spring Boot)](/develop/dev-guide-sample-application-spring-boot.md)
- [Golang](/develop/dev-guide-sample-application-golang.md)
- [Java (Spring Boot)](/develop/dev-guide-sample-application-spring-boot.md)
- [Java](/develop/dev-guide-sample-application-java.md)
- [Python](/develop/dev-guide-sample-application-python.md)
- Connect to TiDB
- [Choose Driver or ORM](/develop/dev-guide-choose-driver-or-orm.md)
- [Connect to TiDB](/develop/dev-guide-connect-to-tidb.md)
Expand Down Expand Up @@ -76,8 +77,6 @@
- [SQL Development Specifications](/develop/dev-guide-sql-development-specification.md)
- Legacy Docs
- [For Django](/develop/dev-guide-outdated-for-django.md)
- [For SQLAlchemy](/develop/dev-guide-outdated-for-sqlalchemy.md)
- [For mysql-connector-python](/develop/dev-guide-outdated-for-python-mysql-connector.md)
- Cloud Native Development Environment
- [Gitpod](/develop/dev-guide-playground-gitpod.md)
- Third-Party Support
Expand Down
72 changes: 70 additions & 2 deletions develop/dev-guide-choose-driver-or-orm.md
Expand Up @@ -9,8 +9,8 @@ summary: Learn how to choose a driver or ORM framework to connect to TiDB.
>
> TiDB provides the following two support levels for drivers and ORMs:
>
> - **Full**: indicates that using this driver or ORM does not have any known issues.
> - **Verified**: indicates that using this driver or ORM might get errors because of compatibility differences between TiDB and MySQL.
> - **Full**: indicates that TiDB is compatible with most features of the tool and maintains compatibility with its newer versions. PingCAP will periodically conduct compatibility tests with the latest version of [Third-party tools supported by TiDB](/develop/dev-guide-third-party-support.md).
> - **Compatible**: indicates that because the corresponding third-party tool is adapted to MySQL and TiDB is highly compatible with the MySQL protocol, so TiDB can use most features of the tool. However, PingCAP has not completed a full test on all features of the tool, which might lead to some unexpected behaviors.
>
> For more information, refer to [Third-Party Tools Supported by TiDB](/develop/dev-guide-third-party-support.md).
Expand Down Expand Up @@ -233,3 +233,71 @@ go get -u gorm.io/driver/mysql
```

For an example of using GORM to build a TiDB application, see [Build a Simple CRUD App with TiDB and Golang](/develop/dev-guide-sample-application-golang.md).
<<<<<<< HEAD
=======

## Python

This section describes how to use drivers and ORM frameworks in Python.

### Python drivers

<SimpleTab>
<div label="PyMySQL">

Support level: **Compatible**

You can follow the [PyMySQL documentation](https://pypi.org/project/PyMySQL/) to download and configure the driver. It is recommended to use PyMySQL 1.0.2 or later versions.

For an example of using PyMySQL to build a TiDB application, see [Build a Simple CRUD App with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code).

</div>
<div label="mysqlclient">

Support level: **Compatible**

You can follow the [mysqlclient documentation](https://pypi.org/project/mysqlclient/) to download and configure the driver. It is recommended to use mysqlclient 2.1.1 or later versions.

For an example of using mysqlclient to build a TiDB application, see [Build a Simple CRUD App with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code).

</div>
<div label="mysql-connector-python">

Support level: **Compatible**

You can follow the [mysql-connector-python documentation](https://dev.mysql.com/doc/connector-python/en/connector-python-installation-binary.html) to download and configure the driver. It is recommended to use Connector/Python 8.0.31 or later versions.

For an example of using mysql-connector-python to build a TiDB application, see [Build a Simple CRUD App with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code).

</div>
</SimpleTab>

### Python ORM frameworks

<SimpleTab>
<div label="SQLAlchemy">

Support level: **Compatible**

[SQLAlchemy](https://www.sqlalchemy.org/) is a popular ORM framework for Python. To get all dependencies in your application, you can use the `pip install SQLAlchemy==1.4.44` command. It is recommended to use SQLAlchemy 1.4.44 or later versions.

For an example of using SQLAlchemy to build a TiDB application, see [Build a Simple CRUD App with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code).

</div>
<div label="peewee">

Support level: **Compatible**

[peewee](http://docs.peewee-orm.com/en/latest/) is a popular ORM framework for Python. To get all dependencies in your application, you can use the `pip install peewee==3.15.4` command. It is recommended to use peewee 3.15.4 or later versions.

For an example of using peewee to build a TiDB application, see [Build a Simple CRUD App with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code).

</div>
</SimpleTab>

<CustomContent platform="tidb-cloud">

After you have determined the driver or ORM, you can [connect to your TiDB cluster](https://docs.pingcap.com/tidbcloud/connect-to-tidb-cluster).

</CustomContent>
>>>>>>> 5e7d54a6a (develop: Develop Guide Python Examples - English Edition (#11742))
59 changes: 59 additions & 0 deletions develop/dev-guide-delete-data.md
Expand Up @@ -141,6 +141,33 @@ func main() {

</div>

<div label="Python" value="python">

In Python, the example is as follows:

```python
import MySQLdb
import datetime
import time
connection = MySQLdb.connect(
host="127.0.0.1",
port=4000,
user="root",
password="",
database="bookshop",
autocommit=True
)
with connection:
with connection.cursor() as cursor:
start_time = datetime.datetime(2022, 4, 15)
end_time = datetime.datetime(2022, 4, 15, 0, 15)
delete_sql = "DELETE FROM `bookshop`.`ratings` WHERE `rated_at` >= %s AND `rated_at` <= %s"
affect_rows = cursor.execute(delete_sql, (start_time, end_time))
print(f'delete {affect_rows} data')
```

</div>

</SimpleTab>

<CustomContent platform="tidb">
Expand Down Expand Up @@ -315,6 +342,38 @@ In each iteration, `DELETE` deletes up to 1000 rows from `2022-04-15 00:00:00` t

</div>

<div label="Python" value="python">

In Python, the bulk-delete example is as follows:

```python
import MySQLdb
import datetime
import time
connection = MySQLdb.connect(
host="127.0.0.1",
port=4000,
user="root",
password="",
database="bookshop",
autocommit=True
)
with connection:
with connection.cursor() as cursor:
start_time = datetime.datetime(2022, 4, 15)
end_time = datetime.datetime(2022, 4, 15, 0, 15)
affect_rows = -1
while affect_rows != 0:
delete_sql = "DELETE FROM `bookshop`.`ratings` WHERE `rated_at` >= %s AND `rated_at` <= %s LIMIT 1000"
affect_rows = cursor.execute(delete_sql, (start_time, end_time))
print(f'delete {affect_rows} data')
time.sleep(1)
```

In each iteration, `DELETE` deletes up to 1000 rows from `2022-04-15 00:00:00` to `2022-04-15 00:15:00`.

</div>

</SimpleTab>

## Non-transactional bulk-delete
Expand Down
44 changes: 37 additions & 7 deletions develop/dev-guide-insert-data.md
Expand Up @@ -113,11 +113,11 @@ The following is a typical scenario of JDBC connection string configurations. In
jdbc:mysql://127.0.0.1:4000/test?user=root&useConfigs=maxPerformance&useServerPrepStmts=true&prepStmtCacheSqlLimit=2048&prepStmtCacheSize=256&rewriteBatchedStatements=true&allowMultiQueries=true
```

For a complete example in Java, see:
For complete examples in Java, see:

- [Build a Simple CRUD App with TiDB and Java - Using JDBC](/develop/dev-guide-sample-application-java.md#step-2-get-the-code)
- [Build a Simple CRUD App with TiDB and Java - Using Hibernate](/develop/dev-guide-sample-application-java.md#step-2-get-the-code)
- [Build the TiDB Application using Spring Boot](/develop/dev-guide-sample-application-spring-boot.md)
- [Build a simple CRUD application with TiDB and Java - using JDBC](/develop/dev-guide-sample-application-java.md#step-2-get-the-code)
- [Build a simple CRUD application with TiDB and Java - using Hibernate](/develop/dev-guide-sample-application-java.md#step-2-get-the-code)
- [Build the TiDB application using Spring Boot](/develop/dev-guide-sample-application-spring-boot.md)

</div>

Expand Down Expand Up @@ -189,10 +189,40 @@ func buildBulkInsertSQL(amount int) string {
}
```

For a complete example in Golang, see:
For complete examples in Golang, see:

- [Use go-sql-driver/mysql to build a simple CRUD app with TiDB and Golang](/develop/dev-guide-sample-application-golang.md#step-2-get-the-code)
- [Use GORM to build a simple CRUD app with TiDB and Golang](/develop/dev-guide-sample-application-java.md#step-2-get-the-code)
- [Use go-sql-driver/mysql to build a simple CRUD application with TiDB and Golang](/develop/dev-guide-sample-application-golang.md#step-2-get-the-code)
- [Use GORM to build a simple CRUD application with TiDB and Golang](/develop/dev-guide-sample-application-java.md#step-2-get-the-code)

</div>

<div label="Python">

```python
import MySQLdb
connection = MySQLdb.connect(
host="127.0.0.1",
port=4000,
user="root",
password="",
database="bookshop",
autocommit=True
)

with get_connection(autocommit=True) as connection:
with connection.cursor() as cur:
player_list = random_player(1919)
for idx in range(0, len(player_list), 114):
cur.executemany("INSERT INTO player (id, coins, goods) VALUES (%s, %s, %s)", player_list[idx:idx + 114])
```

For complete examples in Python, see:

- [Use PyMySQL to build a simple CRUD application with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code)
- [Use mysqlclient to build a simple CRUD application with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code)
- [Use mysql-connector-python to build a simple CRUD application with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code)
- [Use SQLAlchemy to build a simple CRUD application with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code)
- [Use peewee to build a simple CRUD application with TiDB and Python](/develop/dev-guide-sample-application-python.md#step-2-get-the-code)

</div>

Expand Down

0 comments on commit 13cdc87

Please sign in to comment.