Skip to main content

MySQL-statement-builder (mysqlsb)

...

Installation

pip install mysql-statement-builder

Usage

import mysql.connector
from mysql.connector import pooling
from contextlib import contextmanager

from mysqlsb import MySQLStatementBuilder, FetchType

# Setup a connection pool using the MySQL python connector
connection_pool = mysql.connector.pooling.MySQLConnectionPool(
    user='user',
    password='password',
    host='host',
    database='database',
    port='port',
    autocommit=False,
)


# Setup a method for recieving a connection from the connection pool
@contextmanager
def get_connection() -> pooling.PooledMySQLConnection:
    """
    Returns a MySQL connection that can be used for read/write.
    """
    connection = connection_pool.get_connection()
    try:
        yield connection
    finally:
        connection.close()


# Apply mysql-statement-builder to select data from the database
def get_user_from_database(user_id: int):
    with get_connection() as connection:
        mysql_statement = MySQLStatementBuilder(connection)
        user_data = mysql_statement \
            .select('users', ['id', 'username', 'email']) \
            .where('id = %s', [user_id]) \
            .execute(fetch_type=FetchType.FETCH_ONE, dictionary=True)

        if user_data is None:
            raise UserNotFoundException('User could not be found in database')

        user = User(**user_data)  # Unpack results dictionary into e.g. a Pydantic basemodel class
        return user

Metadata

Release files for mysql-statement-builder 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mysql-statement-builder 0.3.0
File Size Uploaded Provenance Actions
mysql-statement-builder-0.3.0.tar.gz 7.2 kB Without provenance Details for mysql-statement-builder-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mysql-statement-builder 0.3.0
File Interpreter ABI Platform Actions
mysql_statement_builder-0.3.0-py3-none-any.whl Python 3 none any Details for mysql_statement_builder-0.3.0-py3-none-any.whl

Total release size: 16.2 kB

Release files / mysql-statement-builder-0.3.0.tar.gz

Download URL mysql-statement-builder-0.3.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6a445888167052df24e2c2f5092f088eb3459ea00503086c94745f28765a14e8
BLAKE2b-256 checksum
How to use checksums
83c317ca7cb8cb7844a644452d17a2d0a51abe76752184917e7aec174cd78915
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / mysql_statement_builder-0.3.0-py3-none-any.whl

Download URL mysql_statement_builder-0.3.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6203f946a47d2b8de9d69d08d1f2381ba428e5e7b748119952928274e7c44126
BLAKE2b-256 checksum
How to use checksums
b3372f8c9fe9d31221cb4def7a0de7d6377045085d257267a8adb0d8e5cc439e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Security for mysql-statement-builder 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page