python-pymysql

Connect to MySQL databases from Python using PyMySQL connect() with charset and cursor settings.

11|2|Updated Feb 18, 2026
One-click install
npx skills add https://github.com/the-perfect-developer/the-perfect-opencode --skill python-pymysql
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-pymysql
Source: https://github.com/the-perfect-developer/the-perfect-opencode/tree/main/.opencode/skills/python-pymysql
Command: npx skills add https://github.com/the-perfect-developer/the-perfect-opencode --skill python-pymysql

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

PyMySQL best practices guide helps developers build reliable, secure MySQL connections in Python by standardizing connection handling, cursor usage, and error management.

Core Features & Use Cases

  • Establish robust connections with proper charset, timeouts, and context management
  • Use parameterized queries to prevent SQL injection and support safe data handling
  • Choose appropriate cursor types and fetch strategies for both small and large result sets
  • Implement transactions, error handling, and cleanup patterns in real-world apps
  • Use configuration from my.cnf and environment variables to avoid hard-coded credentials

Quick Start

Connect to a MySQL database with pymysql.connect by supplying host, user, password, and database, then run a simple query to verify connectivity.

Frequently Asked Questions about python-pymysql

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I connect to a MySQL database in Python using PyMySQL securely?

To connect to MySQL securely with PyMySQL, use pymysql.connect() with environment variables or my.cnf defaults instead of hard-coded credentials, configure proper charset and timeouts, and use context managers for resource cleanup.

How do I prevent SQL injection when querying MySQL with PyMySQL?

To prevent SQL injection in PyMySQL, use parameterized queries instead of string concatenation. Parameterized queries safely separate SQL code from data, ensuring user input is treated strictly as values.

What is the best way to handle database transactions and cleanup with PyMySQL?

The best way to handle PyMySQL transactions and cleanup is using context managers. This pattern ensures proper resource deallocation, manages commits and rollbacks during errors, and safely closes cursors.

When do I need specific cursor types for fetching large MySQL result sets in Python?

Specific PyMySQL cursor types are needed when fetching large MySQL result sets to optimize memory usage. Choosing appropriate cursor types and fetch strategies prevents loading entire datasets into memory.

Can I read MySQL connection defaults from my.cnf when using PyMySQL?

Yes, PyMySQL can read MySQL connection defaults from my.cnf. Reading configuration from my.cnf and environment variables avoids hard-coded credentials and standardizes connection handling.