Advertisement
DrAungWinHtut

mysqloop.py

Jul 20th, 2023
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. import mysql.connector
  2.  
  3. class Database:
  4.     def __init__(self, host, username, password, database):
  5.         self.host = host
  6.         self.username = username
  7.         self.password = password
  8.         self.database = database
  9.         self.connection = None
  10.  
  11.     def connect(self):
  12.         self.connection = mysql.connector.connect(
  13.             host=self.host,
  14.             user=self.username,
  15.             password=self.password,
  16.             database=self.database
  17.         )
  18.  
  19.     def disconnect(self):
  20.         if self.connection:
  21.             self.connection.close()
  22.  
  23.     def execute_query(self, query, values=None):
  24.         cursor = self.connection.cursor()
  25.         if values:
  26.             cursor.execute(query, values)
  27.         else:
  28.             cursor.execute(query)
  29.         self.connection.commit()
  30.         return cursor
  31.  
  32. class User:
  33.     def __init__(self, id, name, email):
  34.         self.id = id
  35.         self.name = name
  36.         self.email = email
  37.  
  38. class CRUDApp:
  39.     def __init__(self, db_config):
  40.         self.db_config = db_config
  41.         self.db = None
  42.  
  43.     def initialize_db(self):
  44.         self.db = Database(**self.db_config)
  45.         self.db.connect()
  46.  
  47.     def close_db(self):
  48.         self.db.disconnect()
  49.  
  50.     def create_user(self, name, email):
  51.         query = "INSERT INTO users (name, email) VALUES (%s, %s)"
  52.         values = (name, email)
  53.         self.db.execute_query(query, values)
  54.  
  55.     def read_users(self):
  56.         query = "SELECT id, name, email FROM users"
  57.         result = self.db.execute_query(query)
  58.         users = []
  59.         for row in result:
  60.             user = User(*row)
  61.             users.append(user)
  62.         return users
  63.  
  64.     def update_user(self, user_id, name, email):
  65.         query = "UPDATE users SET name = %s, email = %s WHERE id = %s"
  66.         values = (name, email, user_id)
  67.         self.db.execute_query(query, values)
  68.  
  69.     def delete_user(self, user_id):
  70.         query = "DELETE FROM users WHERE id = %s"
  71.         values = (user_id,)
  72.         self.db.execute_query(query, values)
  73.  
  74. # Start Here
  75. if __name__ == "__main__":
  76.     db_config = {
  77.         "host": "localhost",
  78.         "username": "your_username",
  79.         "password": "your_password",
  80.         "database": "your_database_name"
  81.     }
  82.  
  83.     app = CRUDApp(db_config)
  84.     app.initialize_db()
  85.  
  86.     # Create a user
  87.     app.create_user("John Doe", "john.doe@example.com")
  88.  
  89.     # Read users
  90.     users = app.read_users()
  91.     for user in users:
  92.         print(f"ID: {user.id}, Name: {user.name}, Email: {user.email}")
  93.  
  94.     # Update a user
  95.     user_id_to_update = 1
  96.     app.update_user(user_id_to_update, "Jane Smith", "jane.smith@example.com")
  97.  
  98.     # Delete a user
  99.     user_id_to_delete = 2
  100.     app.delete_user(user_id_to_delete)
  101.  
  102.     app.close_db()
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement