db pool for pymysql
Project description
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()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pymysql-dbpool-0.0.1.tar.gz
(4.7 kB
view details)
Built Distribution
File details
Details for the file pymysql-dbpool-0.0.1.tar.gz
.
File metadata
- Download URL: pymysql-dbpool-0.0.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
0431b9b8448a02d82c97dd0193a2aeb6c6155904371937041376dc3a418fee96
|
|
MD5 |
38cc2cde88ba795a34c1be262bac5535
|
|
BLAKE2b-256 |
5e55d8dc3832296be8d5ecf905b511445103ea5e28f85c33b473dd5723dd2183
|
File details
Details for the file pymysql_dbpool-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: pymysql_dbpool-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
49f91cad1407dea687672f8dec141c4a0007580bfa17d94b52d12c8e2be202c0
|
|
MD5 |
e303e03b5fd77c8d402dbcac28dd4800
|
|
BLAKE2b-256 |
b9d9d5bff54e6416eafa8d78f4cb9eaa02b9efcc38fac624d2eb5f9c58a29e34
|