EasyMySQL Documentation

EasyMySQL is a Python library that wraps PyMySQL to give you a clean, minimal API for the most common database operations: insert, select, update and delete. You pass table names and plain dictionaries; the library builds the SQL, manages the cursor, and commits for you.

This is the documentation for the 0.1.9.x release line, the current one on PyPI.

Quick Start

Install the library:

pip install easymysql

Connect and run your first query:

from easymysql.mysql import mysql

db = mysql('localhost', 'root', 'password', 'mydb')

# Insert a row — returns the new AUTO_INCREMENT id
new_id = db.insert('users', {
    'name': 'Alice',
    'email': '[email protected]',
})

# Query all users
users = db.select('users')
for user in users:
    print(user['name'], user['email'])

db.close()
Before you build on this

Two things are worth knowing up front: on Python 3.10 and newer the dictionary-condition paths raise AttributeError and update() does not work at all, and no value is ever escaped, so untrusted input must never reach these methods. Both are covered, with workarounds, under Limitations.

API at a glance

Method Returns Purpose
mysql(hostname, username, password, database) instance Connects immediately
insert(table, data) int Inserts a row, returns its id
select(table, condition, fields, order) list[dict] Reads rows
update(table, data, condition) None Updates rows
delete(table, condition) None Deletes rows
query(sql) list[dict] Runs arbitrary SQL that returns rows
execute(sql) None Runs arbitrary SQL with no result set
count() int Rows affected or returned by the last statement
getLastId() int Last AUTO_INCREMENT id
ping(), connect(), reconnect(), close() Connection lifecycle

truncate(), resetCache() and version() also exist but do not behave as their names suggest — see Raw SQL & Utilities.

What's inside