Skip to main content

A unified SQLAlchemy wrapper for SQLite and MySQL

Project description

pymysqlhelper

A unified SQLAlchemy wrapper for SQLite and MySQL.


Overview

pymysqlhelper provides two classes:

  • LocalDatabase (SQLite)
  • Database (MySQL)

Both expose an identical API, allowing you to:

  • Develop locally using SQLite
  • Switch to MySQL in production
  • Change only the constructor — no other code modifications needed

All functionality is shared through a common _DatabaseMixin, ensuring consistency across both backends.


Installation

pip install pymysqlhelper

All dependencies are installed automatically (sqlalchemy, pymysql)


Quick Start

SQLite (Local Development)

from pymysqlhelper import LocalDatabase, Integer, String, Text

db = LocalDatabase("myapp.db")

db.define_table(
    "users",
    id=Integer,
    name=String(100),
    email=Text,
)

db.insert("users", id=1, name="Alice", email="alice@example.com")
db.insert("users", id=1, name="Alice Updated", email="alice@example.com").replace()
db.insert("users", id=1, name="Alice", email="alice@example.com").ignore()

user = db.get("users", id=1)
print(user)

MySQL (Production)

from pymysqlhelper import Database

db = Database("root", "secret", "localhost", 3306, "myapp")

db.define_table("users", id=Integer, name=String(100), email=Text)
db.insert("users", id=1, name="Alice", email="alice@example.com")

Connecting

SQLite

LocalDatabase("local.db")     # File-based
LocalDatabase(":memory:")     # In-memory (testing)

MySQL

Database(
    username="user",
    password="password",
    host="localhost",
    port=3306,
    database="myapp",
)

Defining Tables

from sqlalchemy import Integer, String, Text, Float, ForeignKey

db.define_table(
    "orders",
    id=Integer,
    user_id=(Integer, ForeignKey("users.id")),
    total=Float(),
    note=Text,
)
  • First column is always the primary key (autoincrement=False)
  • Safe to call multiple times (no-op if table exists)

Supported Types

  • Integer, BigInteger, SmallInteger
  • Boolean
  • Float, DECIMAL
  • String(n), Text
  • DateTime, Date, Time
  • LargeBinary

Inserting Data

db.insert("users", id=1, name="Alice")
db.insert("users", id=1, name="Alice").ignore()
db.insert("users", id=1, name="Bob").replace()

Bulk Insert

db.bulk_insert("users", [
    {"id": 2, "name": "Bob"},
    {"id": 3, "name": "Carol"},
])

Querying Data

Multiple Rows

db.search("users")
db.search("users", name="Alice")

Single Row

db.get("users", id=1)

Pagination

db.search_paginated("users", page=2, page_size=20)

Other Helpers

db.count_rows("users")
db.distinct_values("users", "role")

Updating & Deleting

db.update("users", filters={"id": 1}, updates={"name": "Alicia"})
db.delete("users", id=1)

Schema Inspection

db.list_tables()
db.list_columns("users")
db.get_table_schema("users")
db.get_column_type("users", "name")

Schema Modifications

db.add_column("users", "age", Integer)
db.drop_column("users", "bio")
db.edit_column_type("users", "age", Integer)
db.rename_table("users", "accounts")
db.delete_table("temp")

Reload Schema

db.reload()

Use this if schema changes outside the library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymysqlhelper-1.11.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymysqlhelper-1.11.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file pymysqlhelper-1.11.0.tar.gz.

File metadata

  • Download URL: pymysqlhelper-1.11.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pymysqlhelper-1.11.0.tar.gz
Algorithm Hash digest
SHA256 6e0b946df15cc1afe240702891f66315d62087e29fcff1722e9ca1dfcc73302e
MD5 b7cb06c4124cff4696aba4bdf2fa0a63
BLAKE2b-256 a506c1a1f49f89a63bbc56a37f7a515c8f0f9f9a54d729eb8ae4f03eed925cb8

See more details on using hashes here.

File details

Details for the file pymysqlhelper-1.11.0-py3-none-any.whl.

File metadata

  • Download URL: pymysqlhelper-1.11.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pymysqlhelper-1.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5cb64ead5400026f8d95b3286322ffa8f352b58ce04a23c68329422783a593f3
MD5 5b87ce4d7fbd225cf7b90a84d78df68a
BLAKE2b-256 c0c82b4ba2674491215d0c285b3fece97d636d6e42033583a1a0d2ba06addda0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page