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 statement execution from data retrieval.

Install

# Install from PyPI
pip install asmysql

Documentation

Quick Start

import asyncio
from asmysql 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,authentication_string,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()

    await mysql.get_users()

    # Remember to disconnect the MySQL connection before exiting the program:
    await mysql.disconnect()


asyncio.run(main())

Support for asynchronous context managers.

import asyncio
from asmysql import AsMysql

class TestAsMysql(AsMysql):
    async def get_users(self):
        result = await self.client.execute('select user,authentication_string,host from mysql.user')
        if result.err:
            print(result.err)
        else:
            async for item in result.iterate():
                print(item)

async def main():
    # Using async with will automatically close the MySQL connection when the code exits.
    async with TestAsMysql() as mysql:
        await mysql.get_users()

if __name__ == '__main__':
    asyncio.run(main())

More Usage

import asyncio
from asmysql import AsMysql

class TestAsMysql(AsMysql):
    async def get_users(self):
        result = await self.client.execute('select user,authentication_string,host from mysql.user')
        if result.err:
            print(result.err)
        else:
            return await result.fetch_all()

# When creating a MySQL instance, parameters such as MySQL address and user password can be passed in.
mysql = TestAsMysql(host='192.168.1.192', port=3306)

async def main():
    # This will connect to MySQL:
    await mysql.connect()  # or: await mysql

    print(await mysql.get_users())

    # Disconnect MySQL connection:
    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-1.3.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

asmysql-1.3.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for asmysql-1.3.1.tar.gz
Algorithm Hash digest
SHA256 06f09a20d11fabf329e0bf0c89a25d293a9a5e357c6c4247dfe786de8e05ba5d
MD5 e2f433883086cfd36cc46e7954932b26
BLAKE2b-256 3f02efe3bb37d97422f5b89fe2b48a354200af4dce09774ade7935e9cfb45c89

See more details on using hashes here.

Provenance

The following attestation bundles were made for asmysql-1.3.1.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-1.3.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for asmysql-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b52f9532583621f0faf3d036df283ec890c496753a7cdd947536aca75d804e3
MD5 35b8a9290d76a50ce082b36b7cf58fc8
BLAKE2b-256 7d2c86096d90f34fcbf4c7baffa70e9b58cd5061781883b0cec46aa91cbbb8d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for asmysql-1.3.1-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