Skip to main content

An Asynchronous MySQL Client Engine Using Aiomysql.

Project description

asmysql

PyPI Python Licence Downloads Downloads Downloads

Introduction

asmysql is a library for using the MySQL asynchronous client, which is a wrapper for aiomysql.

Features

  • Code supports type annotations.
  • Very easy to use, simply inherit the AsMysql class for logical development.
  • Supports automatic management of the MySQL connection pool and reconnection mechanism.
  • Automatically captures and handles MysqlError errors globally.
  • Separates MySQL connection engine and development logic class.
  • Separates statement execution from data retrieval.
  • Supports uncached data stream acquisition for large data result sets (without occupying memory).

Install

# Install from PyPI
pip install asmysql

Documentation

Quick Start v2

Using Engine class for MySQL connection:

import asyncio

from asmysql import Engine

# Create MySQL connection engine
engine = Engine(url="mysql://root:pass@127.0.0.1:3306/?charset=utf8mb4")


async def main():
    # Connect to MySQL
    await engine.connect()
    # Execute SQL statement
    async with engine.execute("select user,host from mysql.user") as result:
        async for item in result.iterate():
            print(item)
    # Disconnect MySQL connection
    await engine.disconnect()


asyncio.run(main())

Using AsMysql class for logical development:

import asyncio
from asmysql import Engine
from asmysql import AsMysql

# Write logical development class
class TestAsMysql(AsMysql):
    async def print_users(self):
        result = await self.client.execute('select user,host from mysql.user')
        if result.error:
            print(f"error_no: {result.error_no}, error_msg:{result.error_msg}")
        else:
            # result.iterate() is an asynchronous iterator that can fetch each row of the execution result.
            async for item in result.iterate():
                print(item)

async def main():
    # Create MySQL connection engine
    engine = Engine(host='192.168.1.192', port=3306)
    # Connect to MySQL
    await engine.connect()
    # Create logical development class instance
    test_mysql = TestAsMysql(engine)
    # Execute logic
    await test_mysql.print_users()
    # Disconnect MySQL connection
    await engine.disconnect()

asyncio.run(main())

Compatible with v1 version

import asyncio
from asmysql.v1 import AsMysql

# Directly inherit the AsMysql class for development:
class TestAsMysql(AsMysql):
    # You can define some default parameters for the Mysql instance initialization here
    # The attributes are consistent with the __init__ parameters
    host = '127.0.0.1'
    port = 3306
    user = 'root'
    password = 'pass'

    async def get_users(self):
        # The self.client attribute is specifically used to execute SQL statements, providing aiomysql's execute and execute_many methods.
        result = await self.client.execute('select user,host from mysql.user')
        # result is specifically used to obtain execution results, providing fetch_one, fetch_many, fetch_all, and iterate methods.
        # result.err is the exception object (Exception) for all MySQL execution errors.
        if result.err:
            print(result.err_msg)
        else:
            # result.iterate() is an asynchronous iterator that can fetch each row of the execution result.
            async for item in result.iterate():
                print(item)

async def main():
    # This will create an instance and connect to MySQL:
    mysql = await TestAsMysql()
    # Execute SQL statement
    await mysql.get_users()
    # Remember to disconnect the MySQL connection before exiting the program:
    await mysql.disconnect()

asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asmysql-2.0.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asmysql-2.0.0-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file asmysql-2.0.0.tar.gz.

File metadata

  • Download URL: asmysql-2.0.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asmysql-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b515860ee575e39d7b5c6e81f84e0989f07f7693ba660d7ca6d75bcc0c8a95a8
MD5 08121b9229fa38d7fc5736ff69201c72
BLAKE2b-256 1309ae14ad65d0f7475b75aa7ebdf352683ea9c97ad68cbce3b87b05680876c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for asmysql-2.0.0.tar.gz:

Publisher: python_package_publish_to_pypi.yml on Vastxiao/asmysql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file asmysql-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: asmysql-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asmysql-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 294a5004bfe0f319165a30f86f09519e8efbbf7823cc3433e402f66e2dd23211
MD5 880d2c8aac80dda834826fe9064dcdd9
BLAKE2b-256 32e687216b81af76e0f8702b3e5fe49282f9a7182551b96b3b6efd93eaccd6bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for asmysql-2.0.0-py3-none-any.whl:

Publisher: python_package_publish_to_pypi.yml on Vastxiao/asmysql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page