Skip to main content

The library to work with MySQL databases for Python

Project description

PyWithMySQL

PyWithMySQL is a simple and easy-to-use Python library for working with MySQL databases. It provides methods for connecting to the database, performing CRUD operations, and handling MySQL queries efficiently.

Installation

You can install the library using pip:

pip install pywithmysql

Features

Easy connection setup with MySQL Supports basic CRUD operations (Create, Read, Update, Delete) Flexible and extensible methods Error handling for database operations

Usage

Here is a quick example of how to use the library:

from pywithmysql.settings import SettingsTable
from pywithmysql.config import DataBaseConfig

# Define your database configuration
config = DataBaseConfig(
    host="localhost",
    port=3306,
    user="root",
    password="password",
    db_name="test_db"
)

# Initialize the settings table with the table name
table = SettingsTable(config, "users")

# Connect to the database
table.connect()

# Create a new entry in the table
table.create(names=["name", "age"], values=["'Alice'", "30"])

# Read all entries from the table
rows = table.read_all()
for row in rows:
    print(row)

# Update an entry
table.update_data(names=("age",), values=("31",), id=1)

# Delete an entry by id
table.delete_for_id(id=1)

# Disconnect from the database
table.disconnect()

Creating a Table

To create a table, use the Table class along with the Column class to define columns. Here’s a code example:

from pywithmysql.models import Table, Column
from pywithmysql.types import IntegerField, CharField
from pywithmysql.config import DataBaseConfig

# Database connection settings
config = DataBaseConfig(
    host="localhost",
    port=3306,
    user="root",
    password="29062008Kl!",
    db_name="test"
)

# Initialize Table
table = Table(
    name="users",
    config=config,
    columns=[
        Column(name="username", field_type=CharField, max_length=45, nullable=False, unique=True),
        Column(name="age", field_type=IntegerField, default=18),
        Column(name="email", field_type=CharField, max_length=100, unique=True, nullable=False),
        Column(name="phone_number", field_type=CharField, unique=True)
    ]
)

# Create the table
table.create_table()

Methods

connect() Establishes a connection to the MySQL database using the provided configuration.

create(names: list, values: list) Inserts a new row into the table. You must provide column names and their corresponding values.

read_all() Fetches all rows from the table.

update_data(names: tuple, values: tuple, id: int) Updates specific fields for a row with the given id.

delete_for_id(id: int) Deletes a row from the table based on the provided id.

disconnect() Closes the connection to the MySQL database.

License

This project is licensed under the MIT License.

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

pywithmysql-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

pywithmysql-0.1.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pywithmysql-0.1.2.tar.gz.

File metadata

  • Download URL: pywithmysql-0.1.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for pywithmysql-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2a8a04aae38735f8806fb963ecfc7f6905b45b6ca57382ce2eae27a5a1b5f1e0
MD5 7cc0a170da016e4414427d794d7e2d0f
BLAKE2b-256 48e6491b4725edc8e29b24b2a325eb9088b7337c13f26806c097c7e8b21975fe

See more details on using hashes here.

File details

Details for the file pywithmysql-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pywithmysql-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for pywithmysql-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2a7662019a4408369db10369a329fcc7dee238d17e1e88769ade0beba074b34e
MD5 ac58e0c777f7aa3d3c0aaaa2f3e07721
BLAKE2b-256 dc00fb96e9548fce23d340dece4fdbcf6a8b2c338986aa41d4b184ae95551667

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