pyro_mysql
pyro_mysql - High-performance MySQL driver for Python.
import asyncio
import pyro_mysql as mysql
mysql.init(worker_threads=1)
async def example_select():
conn = await mysql.Conn.new("mysql://localhost@127.0.0.1:3306/test")
rows = await conn.exec("SELECT * from mydb.mytable")
print(row[-1].to_dict())
async def example_transaction():
conn = await mysql.Conn.new("mysql://localhost@127.0.0.1:3306/test")
async with conn.start_transaction() as tx:
await tx.exec_drop(
"INSERT INTO test.asyncmy(`decimal`, `date`, `datetime`, `float`, `string`, `tinyint`) VALUES (?,?,?,?,?,?)",
(
1,
"2021-01-01",
"2020-07-16 22:49:54",
1,
"asyncmy",
1,
),
)
await tx.commit()
await len(conn.exec('SELECT * FROM mydb.mytable')) == 100
# The connection pool is not tied to a single event loop.
# You can reuse the pool between event loops.
asyncio.run(example_pool())
asyncio.run(example_select())
asyncio.run(example_transaction())
...
Initialize the Tokio runtime for async operations. This function can be called multiple times until Any async operation is called.
Arguments:
- worker_threads: Number of worker threads for the Tokio runtime. Defaults to the number of CPUs.
- thread_name: Name prefix for worker threads.
A row returned from a MySQL query. to_list() / to_dict() copies the data, and should not be called many times.
MySQL connection pool.
MySQL connection.
The API is thread-safe. The underlying implementation is protected by RwLock.
Create a new connection.
Arguments:
- url: MySQL connection URL (e.g., 'mysql://user:password@host:port/database').
Returns:
New Conn instance.
Close a prepared statement (not yet implemented).
Execute a query and return all rows.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Returns:
List of Row objects.
Execute a query and return the first row.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Returns:
First Row or None if no results.
Execute a query and discard the results.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Represents a MySQL transaction with async context manager support.
Close a prepared statement (not yet implemented).
Execute a query and return all rows.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Returns:
List of Row objects.
Execute a query and return the first row.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Returns:
First Row or None if no results.
Execute a query and discard the results.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Transaction isolation level enum.
Synchronous MySQL connection.
Create a new synchronous connection.
Arguments:
- url: MySQL connection URL (e.g., 'mysql://user:password@host:port/database').
Run a transaction with a callable.
Arguments:
- callable: A callable that will receive the transaction object.
- consistent_snapshot: Whether to use consistent snapshot.
- isolation_level: Transaction isolation level.
- readonly: Whether the transaction is read-only.
Returns:
The return value of the callable.
Execute a query and return all rows.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Returns:
List of Row objects.
Execute a query and return the first row.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Returns:
First Row or None if no results.
Execute a query and discard the results.
Arguments:
- query: SQL query string with '?' placeholders.
- params: Query parameters.
Type alias for the purpose of documenation.
These Python types can be converted to MySQL values:
- None
- bool
- int
- float
- str
- bytes
- bytearray
- tuple[Any, ...]
- list[Any]
- dict[str, Any]
- datetime.datetime
- datetime.date
- datetime.time
- datetime.timedelta
- time.struct_time
- decimal.Decimal
Type alias for the purpose of documenation.
Parameters that can be passed to query execution methods:
- None: No parameters
- tuple[Value, ...]: Positional parameters for queries with ? placeholders
- list[Value]: List of parameters for queries with ? placeholders
- dict[str, Value]: Named parameters for queries with named placeholders
Examples:
No parameters:
await conn.exec("SELECT * FROM users")
Positional parameters:
await conn.exec("SELECT * FROM users WHERE id = ?", (123,))
Multiple positional parameters:
await conn.exec("SELECT * FROM users WHERE age > ? AND city = ?", (18, "NYC"))
Named parameters:
await conn.exec("SELECT * FROM users WHERE age > :age AND city = :city", dict(age=18, name="NYC"))