Giter Site home page Giter Site logo

bogdankuhar / aiomysql_crud Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 9 KB

A versatile Python class utilizing the aiomysql library for streamlined interaction with MySQL databases. Simplifies connection management and CRUD operations for convenience and efficiency.

License: Other

Python 100.00%
aiomysql crud database mysql python

aiomysql_crud's Introduction

aiomysql_CRUD

A versatile Python class utilizing the aiomysql library for streamlined interaction with MySQL databases. Simplifies connection management and CRUD operations for convenience and efficiency.

Database Interaction with aiomysql

This project provides a Python class, aiomysql_CRUD, that simplifies interaction with MySQL databases using the aiomysql library. It offers convenient methods for performing CRUD (Create, Read, Update, Delete) operations on database tables.

Features

  • Asynchronous database operations for improved performance.
  • Easy integration into Python projects.
  • Streamlined connection management.
  • Support for common database operations, including select, insert, update, and delete.

Getting Started

  1. Clone the repository:
    git clone https://github.com/BogdanKuhar/aiomysql_CRUD
    cd your-project
    
    

Install the required dependencies:

pip install aiomysql

Modify the config section in db.py to match your database settings.

Use the Database class in your Python project to interact with your MySQL database.

Usage Here's an example of how to use the "aiomysql_CRUD" class to perform basic database operations:

import asyncio from db import aiomysql_CRUD

async def main(): db = aiomysql_CRUD() await db.connect()

# INSERT - The code performs an INSERT operation into the users table, inserting a record with the name "John Doe" and age 30.
async def create_user(user_name, user_age):
    query = "INSERT INTO users (user_name, user_age) VALUES (%s, %s)"
    values = (user_name, user_age)
    await db.insert(query, values)

# Now you can call the create_user function within an asynchronous context
await create_user("John Doe", 35)


# UPDATE - Updates a user's age to 55 based on their name ("John Doe").
async def update_user(user_name, user_age):
    query = "UPDATE users SET user_age = %s WHERE user_name = %s"
    values = (user_age, user_name)  # Swap the values to match the order in the query
    await db.update(query, values)

# This line of code calls the `update_user` function to change the age of a user named "John Doe" to 55 in the database.
await update_user("John Doe", 55)


# SELECT - This code defines a function `select_users_by_age` that retrieves users from the "users" table whose age is greater than the specified minimum age.
# It then prints the selected users.
async def select_users_by_age(min_age):
    query = "SELECT id, user_name, user_age FROM users WHERE user_age > %s"
    values = (min_age,)
    result = await db.select(query, values)
    return result

min_age = 30  # Adjust this value to your desired minimum age
users = await select_users_by_age(min_age)

for user in users:
    print(user)

# DELETE - Deleting a user from the users table based on their name.
async def delete_user(user_name):
    query = "DELETE FROM users WHERE user_name = %s"
    values = (user_name,)
    await db.delete(query, values)


# Calling the function to delete a user "John Doe" from the users table.
await delete_user("John Doe")



await db.close()

if name == "main": asyncio.run(main())

Contributing Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.

License This project is licensed under the MIT License - see the LICENSE file for details.

aiomysql_crud's People

Contributors

bogdankuhar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.