API Reference
PyMySQL
A pure-Python MySQL client library that speaks the MySQL protocol without native dependencies.
Official Documentation ↗CompatibilityThis documentation covers version
v1.1. Check the official docs for the latest API surface.Installation
pip
pip install pymysqlMethods
No methods match this filter.
PyMySQL implements the MySQL client protocol in pure Python and exposes a DB-API connection.
Key Features
- Pure Python, no C extension
- DB-API cursor interface
- Parameterized queries with %s placeholders
Usage Notes
Pin a version and check the official docs for the API surface you depend on before upgrading.
Errors
The official API reference declares the following error types:
pymysql.err.Error(exception) — official text: “Raises: Error – If the connection is already closed.” (source: https://pymysql.readthedocs.io/en/latest/modules/connections.html#pymysql.connections.Connection.close, fetched 2026-11-08T16:03:08.4363995+08:00)
Security
Official security statements captured from the package documentation:
- TLS 证书与主机身份校验需显式开启 — “ssl_verify_cert – Set to true to check the server certificate’s validity. ssl_verify_identity – Set to true to check the server’s identity.” (source: https://pymysql.readthedocs.io/en/latest/modules/connections.html#pymysql.connections.Connection, fetched 2026-11-08T16:03:08.4363995+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:03:08.4363995+08:00 via WebFetch 官方 API 页面(Connection / Cursor);connect 别名取自官方仓库 pymysql/init.py.
cursor.execute→ signature from https://pymysql.readthedocs.io/en/latest/modules/cursors.html#pymysql.cursors.Cursor.execute; official text: “execute(query, args=None) — Execute a query. Returns: Number of affected rows. Return type: int”connection.commit→ signature from https://pymysql.readthedocs.io/en/latest/modules/connections.html#pymysql.connections.Connection.commit; official text: “commit() — Commit changes to stable storage. See Connection.commit() in the specification.”
FAQ
- How do I pass parameters?
- Use %s placeholders and a tuple argument. Never format strings into SQL, which would allow injection.
- Why are my changes not saved?
- PyMySQL disables autocommit by default, so you must call commit after writes.
- How do I get the inserted id?
- Read cursor.lastrowid after an INSERT to obtain the auto-increment value.
Report a problem with this API docOpens a pre-filled issue on GitHub.