Python MySQL Driver using Cython for high-performance database connectivity with async support
npx @tessl/cli install tessl/pypi-cymysql@1.1.0A high-performance Python MySQL client library forked from PyMySQL, accelerated using Cython for improved performance. CyMySQL implements the Python Database API Specification v2.0 and supports both synchronous and asynchronous operations through asyncio, with features like connection pooling, prepared statements, and comprehensive MySQL protocol implementation.
pip install cymysqlNO_CYTHON=1 pip install cymysqlimport cymysqlFor dictionary cursors:
from cymysql.cursors import DictCursorFor async operations:
import cymysql.aio
from cymysql.aio import AsyncDictCursorimport cymysql
# Connect to database
conn = cymysql.connect(
host='127.0.0.1',
user='root',
passwd='',
db='database_name'
)
# Execute queries using cursor
cur = conn.cursor()
cur.execute('SELECT foo, bar FROM baz')
# Fetch results
for row in cur.fetchall():
print(row[0], row[1])
# Clean up
cur.close()
conn.close()import asyncio
import cymysql.aio
async def database_operations():
# Single connection
conn = await cymysql.aio.connect(
host="127.0.0.1",
user="root",
passwd="",
db="database_name"
)
cur = conn.cursor()
await cur.execute("SELECT 42")
result = await cur.fetchall()
print(result)
# Connection pool
pool = await cymysql.aio.create_pool(
host="127.0.0.1",
user="root",
passwd="",
db="database_name",
minsize=1,
maxsize=10
)
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT * FROM users")
users = await cur.fetchall()
pool.close()
await pool.wait_closed()
asyncio.run(database_operations())CyMySQL follows the Python DB-API 2.0 specification with performance enhancements:
Core connection functionality for establishing and managing database connections with support for SSL, authentication, character sets, and connection parameters.
def connect(host='localhost', user=None, password='', passwd='', port=3306, db=None, charset='utf8mb4', **kwargs):
"""Create database connection."""SQL execution interface providing methods for running queries, managing transactions, and retrieving results with support for prepared statements and parameterized queries.
class Cursor:
def execute(self, query, args=None): ...
def executemany(self, query, args): ...
def fetchone(self): ...
def fetchall(self): ...
def fetchmany(self, size=None): ...
class DictCursor(Cursor):
"""Cursor that returns rows as dictionaries instead of tuples."""
def fetchone(self): ... # Returns dict
def fetchall(self): ... # Returns tuple of dictsAsyncio-compatible database operations including async connections, cursors, and connection pooling for high-performance concurrent database access.
async def connect(host='localhost', user=None, password='', **kwargs):
"""Create async database connection."""
async def create_pool(minsize=1, maxsize=10, **kwargs):
"""Create async connection pool."""
class AsyncDictCursor:
"""Async cursor that returns rows as dictionaries."""
async def fetchone(self): ... # Returns dict
async def fetchall(self): ... # Returns tuple of dictsComprehensive MySQL data type support with automatic conversion between Python and MySQL types, including support for dates, times, binary data, and JSON.
def escape_string(value): ...
def escape_dict(val, charset): ...
def Binary(x): ...
Date = date
Time = time
Timestamp = datetimeComplete exception hierarchy following DB-API 2.0 specification for handling database errors, operational issues, and programming mistakes.
class MySQLError(Exception): ...
class DatabaseError(MySQLError): ...
class OperationalError(DatabaseError): ...
class ProgrammingError(DatabaseError): ...CyMySQL fully implements Python Database API Specification v2.0: