Skip to main content

pymysql-connection-manager

pymysql connection & pool manager for python3

Refactor pymysql connection

New features

  1. Parameter ‘charset’ default is utf8

  2. Parameter ‘autocommit’ default is True

  3. Added parameter ‘timezone’, default is ‘+00:00’

  4. Use pymysql.cursors.DictCursor by default

  5. Reconnect after the database connection is lost

  6. Add logs for creating connections, mysql warnings, exceptions, database queries, etc.

  7. Using the with…as syntax for transaction operations

  8. Provide simplified query methods such as fetch_all/fetch_row/fetch_column/fetch_first

  9. Provide simplified methods such as insert/insert_many/update/delete

1. Create pymysql connection

import pymysql
from pymysql_manager import Connection

conn = Connection(host='192.0.0.1', database='foo', timezone='+8:00')

2. Transaction

Before code:

try:
  conn.begin()
  conn.execute(....)
catch Exception:
  conn.rollback()
else:
  conn.commit()

Now:

with conn.transaction():
  conn.execute(...)

3. Fetch rowsets

# executed: select * from foo where id between 5 and 10
all_rows = conn.fetch_all('select * from foo where id between %s and %s', 5, 10)

# executed: select * from foo limit 1
first_row = conn.fetch_row('select * from foo')

# executed: select * from foo limit 1
first_column_on_first_row = conn.fetch_first('select * from foo')

# executed: select * from foo limit 1
third_column_on_first_row = conn.fetch_column('select * from foo', column=3)

4. Fetch by Iterator

When a result is large, it may be used SSCursor. But sometimes using limit … offset … can reduce the pressure on the database

by SSCursor

cursor = conn.cursor(pymysql.cursors.SSCursor)
conn.execute(sql)
while True:
  row = cursor.fetchone()
  if not row:
    break

by fetch_iterator

for row in conn.fetch_iterator(sql, per=1000, max=100000):
  print(row)

5. Single/Bulk Insert or Replace | Update | Delete

# insert ignore into mytable (foo, bar) values (1, 2)
db.insert('insert ignore into mytable', foo=1, bar=2)

# insert ignore into mytable (foo, bar) values (1, 2) on duplicate key update ...
db.insert('insert ignore into mytable on duplicate key update ...', **dict(foo=1, bar=2))

# insert ignore into mytable (id, name) values (1, 'foo'), (2, 'bar') on duplicate key update ...
db.insert_many('insert ignore into mytable on duplicate key update ...', ['id', 'name'], [(1, 'foo'), (2, 'bar')])

# update mytable set foo=1, bar=2 where id between %s and %s
db.update('update mytable where id between %s and %s', 10, 5, foo=1, bar=2)
db.update('update mytable where id between %s and %s', [10, 5], foo=1, bar=2)
db.update('update mytable where id between %s and %s', *[10, 5], **dict(foo=1, bar=2))

# update from mytable where id between %s and %s
db.delete('delete from mytable id between %s and %s', 10, 5)
db.delete('delete from mytable id between %s and %s', [10, 5])

Connection Pool

1. Create connection pool

from pymysql_manager import ConnectionPooled
pooled = ConnectionPooled(host='192.0.0.1', database='foo',
                          pool_options=dict(max_size=10, max_usage=100000, idle=60, ttl=120))

2. Execute SQL without the connection pool

pooled.execute(sql)
pooled.connection.execute(sql)

3. Using connection pool to execute SQL

with pooled.pool() as connection:
  connection.execute(sql)

Connection Manager

1. Configuration

from pymysql_manager import ConnectionManager
m = ConnectionManager(default='foo',
                       foo=dict(host='192.0.0.1', database='foo', user='root', passwd=''),
                       bar=dict(host='192.0.0.1', database='bar', user='root', passwd=''))

2. Get a connection

m.execute(sql) # use default connection
m['foo].execute(sql)
m.connection('foo').exeucte(sql)

3. Get a connection from connection pool

with m.pool() as connection: pass  # use default connection
with m['foo'].pool() as connection: pass
with m.connection('foo').pool() as connection: pass

License

The MIT License (MIT). Please see License File for more information.

Metadata

Release files for pymysql-manager 0.0.2

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-manager 0.0.2
File Size Uploaded Provenance Actions
pymysql_manager-0.0.2.tar.gz 8.3 kB Without provenance Details for pymysql_manager-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pymysql-manager 0.0.2
File Interpreter ABI Platform Actions
pymysql_manager-0.0.2-py3-none-any.whl Python 3 none any Details for pymysql_manager-0.0.2-py3-none-any.whl

Total release size: 16.1 kB

Release files / pymysql_manager-0.0.2.tar.gz

Download URL pymysql_manager-0.0.2.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
32a4500756313384601e2bc3cd34b734ac07ae8c2f7ddecff5aaf90ace3a6cd1
BLAKE2b-256 checksum
How to use checksums
851ca90903f3f775fad223f2e9995a3ac04a4b6be19e68508b6434fbb893e648
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pymysql_manager-0.0.2-py3-none-any.whl

Download URL pymysql_manager-0.0.2-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9d43407ea96a4929a651073f24c837d8ea0e3cd6a346df82c79d4d35f55a9238
BLAKE2b-256 checksum
How to use checksums
2cbdea4a95f68987cd7b870fda8cd218eeb6f632586f58e5d0e82963b550495d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pymysql-manager 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

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