AsMysql
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¶
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¶
Engineclass: Independent MySQL connection engine, can be used standaloneAsMysqlclass: Business logic development base class, ready to use by inheritanceResultclass: 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 typedict: Dictionary type- Custom models: Support for Pydantic and other model classes
5. Data Retrieval Methods¶
fetch_one(): Get a single recordfetch_many(): Get multiple recordsfetch_all(): Get all recordsiterate(): 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 withsyntax - 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:
clientattribute: Access toEngineinstance- 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
Related Links¶
- PyPI: pypi.org/project/asmysql
- GitHub: github.com/vastxiao/asmysql
- Gitee: gitee.com/vastxiao/asmysql
- Documentation: vastxiao.github.io/asmysql