Skip to content

AsMysql

PyPI Python License Downloads Monthly Downloads Weekly Downloads

Asynchronous MySQL Client Library

  • asmysql is an asynchronous MySQL client library based on aiomysql, designed for Python asynchronous programming.
  • The v2 version has completely refactored the architecture, providing a clearer and more flexible API design, supporting type hints, connection pool management, error handling, and other enterprise-level features.

🚀 Simple & Easy

Intuitive API design with low learning curve, quick to get started

⚡ High Performance

Asynchronous operations based on connection pool, supporting high concurrency

🔧 Type Safe

Complete type hint support for better development experience

💾 Memory Friendly

Supports streaming queries, handling large datasets without memory consumption

🔌 Flexible Extension

Separated engine and business logic for better architecture design

🪶 Ultra Lightweight

Minimal code size, no redundant dependencies, simple deployment

Quick Start

Installation

pip install asmysql

Quick Example

import asyncio
from asmysql import Engine

async def main():
    # Create engine
    engine = Engine(
        host='localhost',
        port=3306,
        user='root',
        password='password',
        db='test'
    )

    # Connect to database
    await engine.connect()

    # Execute query
    result = await engine.query("SELECT * FROM users WHERE id = %s", (1,))
    user = await result.fetch_one(as_dict=True)
    print(user)

    # Close connection
    await engine.close()

asyncio.run(main())

Core Features

Core Features of v2 Version

1. Separated Architecture

  • Engine class: Independent MySQL connection engine, can be used standalone
  • AsMysql class: Business logic development base class, ready to use by inheritance
  • Result class: Result processing class, supporting multiple data retrieval methods

2. Connection Management

  • Automatic MySQL connection pool management
  • Support for connection pool configuration (min/max connections)
  • Automatic reconnection mechanism
  • Connection status monitoring

3. Flexible Query Methods

  • Support for regular queries and streaming queries
  • Support for single and batch execution
  • Support for transaction control

4. Multiple Result Types

  • tuple: Default tuple type
  • dict: Dictionary type
  • Custom models: Support for Pydantic and other model classes

5. Data Retrieval Methods

  • fetch_one(): Get a single record
  • fetch_many(): Get multiple records
  • fetch_all(): Get all records
  • iterate(): Async iterator, get records row by row
  • Direct iteration: async for item in result

6. Error Handling

  • Global automatic capture of MysqlError
  • Error code and error message access
  • Elegant error handling mechanism

7. Context Management

  • Support for async with syntax
  • Automatic resource cleanup
  • Support for async iterator protocol

8. URL Connection String

  • Support for MySQL URL format connection
  • Format: mysql://user:password@host:port/?charset=utf8mb4

Core Concepts

Engine

Engine is the core class for MySQL connections, responsible for:

  • Managing connection pool
  • Executing SQL statements
  • Handling connection lifecycle

AsMysql (Business Logic Class)

AsMysql is the base class for business logic development, providing:

  • client attribute: Access to Engine instance
  • Business method encapsulation
  • Code organization capability

Result

Result is the encapsulation class for SQL execution results, providing:

  • Data retrieval methods
  • Error information access
  • Execution statistics
  • Async iteration support