Skip to main content

Create Mysql Pool

from pymysqlpool.pooled_db import PooledDB

pool = PooledDB(
    max_connections=10,  # 连接池允许的最大连接数,0和None表示不限制连接数
    set_session=[f'SET time_zone = "+8:00"', 'SET autocommit=1'],  # 开始会话前执行的命令列表。如:["set datestyle to …", "set time zone …"]
    ping=1,  # ping 探活。 0=None=never, 1=default=requested,2=cursor created, 4=query executed,7=always
    host="127.0.0.1",
    port=3306,
    user="root",
    password="admin",
    database="testdb",
    charset="utf8"
)

Execute sql command

select

conn = pool.connect()
try:
    cursor = conn.cursor()
    cursor.execute("select * from table")
    result = cursor.fetchall()
    cursor.close()
finally:
    # 这里回收连接
    conn.close()

insert

创建pool的时候,如果设置了自动提交,即set_session参数包含SET autocommit=1语句, 那么insert之后无需调用commit(),否则需要调用commit()手动提交。

conn = pool.connect()
try:
    cursor = conn.cursor()
    cursor.execute("insert into table value(...)")
    # conn.commit()
    cursor.close()
finally:
    # 这里回收连接
    conn.close()

update

创建pool的时候,如果设置了自动提交,即set_session参数包含SET autocommit=1语句, 那么update之后无需调用commit(),否则需要调用commit()手动提交。

conn = pool.connect()
try:
    cursor = conn.cursor()
    cursor.execute("update table set col='val' where condition")
    # conn.commit()
    cursor.close()
finally:
    # 这里回收连接
    conn.close()

transaction

事务,调用begin()commit()分别开启和提交事务

conn = pool.connect()
try:
    cursor = conn.cursor()
    # start transaction
    conn.begin()
    cursor.execute("update table set col1=val1 where condition")
    cursor.execute("update table set col2=val2 where condition")
    # commit transaction
    conn.commit()
    cursor.close()
finally:
    # 这里回收连接
    conn.close()

Release files for pymysql-dbpool 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymysql-dbpool 0.0.1
File Size Uploaded
pymysql-dbpool-0.0.1.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymysql-dbpool 0.0.1
File Interpreter ABI Platform
pymysql_dbpool-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / pymysql-dbpool-0.0.1.tar.gz

Download URL pymysql-dbpool-0.0.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0431b9b8448a02d82c97dd0193a2aeb6c6155904371937041376dc3a418fee96
BLAKE2b-256 checksum
How to use checksums
5e55d8dc3832296be8d5ecf905b511445103ea5e28f85c33b473dd5723dd2183
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

Release files / pymysql_dbpool-0.0.1-py3-none-any.whl

Download URL pymysql_dbpool-0.0.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
49f91cad1407dea687672f8dec141c4a0007580bfa17d94b52d12c8e2be202c0
BLAKE2b-256 checksum
How to use checksums
b9d9d5bff54e6416eafa8d78f4cb9eaa02b9efcc38fac624d2eb5f9c58a29e34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page