|
| 1 | +title: sqlalchemy.dialects.mysql pymysql Example Code |
| 2 | +category: page |
| 3 | +slug: sqlalchemy-dialects-mysql-pymysql-examples |
| 4 | +sortorder: 500031005 |
| 5 | +toc: False |
| 6 | +sidebartitle: sqlalchemy.dialects.mysql pymysql |
| 7 | +meta: Python example code for the pymysql callable from the sqlalchemy.dialects.mysql module of the SQLAlchemy project. |
| 8 | + |
| 9 | + |
| 10 | +pymysql is a callable within the sqlalchemy.dialects.mysql module of the SQLAlchemy project. |
| 11 | + |
| 12 | + |
| 13 | +## Example 1 from databases |
| 14 | +[databases](https://github.com/encode/databases) |
| 15 | +([project homepage](https://www.encode.io/databases/) |
| 16 | +and |
| 17 | +[PyPI page](https://pypi.org/project/databases/) provides |
| 18 | +[asyncio](https://docs.python.org/3/library/asyncio.html) support |
| 19 | +with an [SQLALchemy](/sqlalchemy.html) Core interface for common |
| 20 | +[relational databases](/databases.html) such as [MySQL](/mysql.html), |
| 21 | +[PostgreSQL](/postgresql.html) and [SQLite](/sqlite.html). This is |
| 22 | +handy for integrating with asynchronous I/O |
| 23 | +[web frameworks](/web-frameworks.html) like [Sanic](/sanic.html). |
| 24 | +The project is open sourced under the |
| 25 | +[BSD 3-Clause "New" or "Revised" License](https://github.com/encode/databases/blob/master/LICENSE.md). |
| 26 | + |
| 27 | +[**databases / databases / backends / mysql.py**](https://github.com/encode/databases/blob/master/databases/backends/mysql.py) |
| 28 | + |
| 29 | +```python |
| 30 | +# mysql.py |
| 31 | +import getpass |
| 32 | +import logging |
| 33 | +import typing |
| 34 | +import uuid |
| 35 | + |
| 36 | +import aiomysql |
| 37 | +~~from sqlalchemy.dialects.mysql import pymysql |
| 38 | +from sqlalchemy.engine.interfaces import Dialect, ExecutionContext |
| 39 | +from sqlalchemy.engine.result import ResultMetaData, RowProxy |
| 40 | +from sqlalchemy.sql import ClauseElement |
| 41 | +from sqlalchemy.types import TypeEngine |
| 42 | + |
| 43 | +from databases.core import LOG_EXTRA, DatabaseURL |
| 44 | +from databases.interfaces import ConnectionBackend, DatabaseBackend, TransactionBackend |
| 45 | + |
| 46 | +logger = logging.getLogger("databases") |
| 47 | + |
| 48 | + |
| 49 | +class MySQLBackend(DatabaseBackend): |
| 50 | + def __init__( |
| 51 | + self, database_url: typing.Union[DatabaseURL, str], **options: typing.Any |
| 52 | + ) -> None: |
| 53 | + self._database_url = DatabaseURL(database_url) |
| 54 | + self._options = options |
| 55 | +~~ self._dialect = pymysql.dialect(paramstyle="pyformat") |
| 56 | + self._dialect.supports_native_decimal = True |
| 57 | + self._pool = None |
| 58 | + |
| 59 | + def _get_connection_kwargs(self) -> dict: |
| 60 | + url_options = self._database_url.options |
| 61 | + |
| 62 | + kwargs = {} |
| 63 | + min_size = url_options.get("min_size") |
| 64 | + max_size = url_options.get("max_size") |
| 65 | + ssl = url_options.get("ssl") |
| 66 | + |
| 67 | + if min_size is not None: |
| 68 | + kwargs["minsize"] = int(min_size) |
| 69 | + if max_size is not None: |
| 70 | + kwargs["maxsize"] = int(max_size) |
| 71 | + if ssl is not None: |
| 72 | + kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()] |
| 73 | + |
| 74 | + for key, value in self._options.items(): |
| 75 | + if key == "min_size": |
| 76 | + key = "minsize" |
| 77 | + elif key == "max_size": |
| 78 | + key = "maxsize" |
| 79 | + kwargs[key] = value |
| 80 | + |
| 81 | + |
| 82 | +## ... source file continues with no further pymysql examples... |
| 83 | + |
| 84 | +``` |
| 85 | + |
0 commit comments