Advertisement
DrAungWinHtut

mysqloopsimple.py

Jul 20th, 2023
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. import mysql.connector
  2.  
  3. class CRUDApp:
  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.     def create_user(self, name, email):
  33.         query = "INSERT INTO users (name, email) VALUES (%s, %s)"
  34.         values = (name, email)
  35.         self.execute_query(query, values)
  36.  
  37.     def read_users(self):
  38.         query = "SELECT id, name, email FROM users"
  39.         result = self.execute_query(query)
  40.         users = []
  41.         for row in result:
  42.             user = User(*row)
  43.             users.append(user)
  44.         return users
  45.  
  46.     def update_user(self, user_id, name, email):
  47.         query = "UPDATE users SET name = %s, email = %s WHERE id = %s"
  48.         values = (name, email, user_id)
  49.         self.execute_query(query, values)
  50.  
  51.     def delete_user(self, user_id):
  52.         query = "DELETE FROM users WHERE id = %s"
  53.         values = (user_id,)
  54.         self.execute_query(query, values)
  55.  
  56. class User:
  57.     def __init__(self, id, name, email):
  58.         self.id = id
  59.         self.name = name
  60.         self.email = email
  61.  
  62. if __name__ == "__main__":
  63.     app = CRUDApp("localhost", "your_username", "your_password", "your_database_name")
  64.     app.connect()
  65.  
  66.     # Create a user
  67.     app.create_user("John Doe", "john.doe@example.com")
  68.  
  69.     # Read users
  70.     users = app.read_users()
  71.     for user in users:
  72.         print(f"ID: {user.id}, Name: {user.name}, Email: {user.email}")
  73.  
  74.     # Update a user
  75.     user_id_to_update = 1
  76.     app.update_user(user_id_to_update, "Jane Smith", "jane.smith@example.com")
  77.  
  78.     # Delete a user
  79.     user_id_to_delete = 2
  80.     app.delete_user(user_id_to_delete)
  81.  
  82.     app.disconnect()
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement