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

pippip install pymysql

Methods

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:

Security

Official security statements captured from the package documentation:

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.

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.