Advertisement
DrAungWinHtut

mysqldb.py

Dec 23rd, 2023 (edited)
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. # this program need myconnection.py https://pastebin.com/MUUE8pM6
  2.  
  3. from myconnection import connect_to_mysql
  4. from icecream import ic
  5.  
  6. def create_database(cursor, database_name):
  7.     query = "CREATE DATABASE IF NOT EXISTS %s"
  8.     cursor.execute(query % (database_name,))
  9.  
  10. def drop_table(cursor, table_name):
  11.     query = "DROP TABLE IF EXISTS %s"
  12.     cursor.execute(query % (table_name,))
  13.  
  14. def rename_table(cursor, old_table_name, new_table_name):
  15.     query = "RENAME TABLE %s TO %s"
  16.     cursor.execute(query % (old_table_name, new_table_name))
  17.  
  18. def create_table(cursor, table_name, table_str):
  19.     query = "CREATE TABLE %s (%s)"
  20.     cursor.execute(query % (table_name, table_str))
  21.  
  22. def create_student(cursor, name, age):
  23.     query = "INSERT INTO students (name, age) VALUES (%s, %s)"
  24.     cursor.execute(query, (name, age))
  25.  
  26. def read_students(cursor):
  27.     query = "SELECT * FROM students"
  28.     cursor.execute(query)
  29.     return cursor.fetchall()
  30.  
  31. def update_student(cursor, student_id, new_name, new_age):
  32.     query = "UPDATE students SET name=%s, age=%s WHERE id=%s"
  33.     cursor.execute(query, (new_name, new_age, student_id))
  34.  
  35. def delete_student(cursor, student_id):
  36.     query = "DELETE FROM students WHERE id=%s"
  37.     cursor.execute(query, (student_id,))
  38.  
  39. def main():
  40.     config = {
  41.         "host": "127.0.0.1",
  42.         "user": "root",
  43.         "password": "",
  44.         "database": "schooldb",
  45.     }
  46.  
  47.     cnx = connect_to_mysql(config, attempts=3)
  48.  
  49.     if cnx and cnx.is_connected():
  50.         with cnx.cursor() as cursor:
  51.             # Create the database if it doesn't exist
  52.             create_database(cursor, 'schooldb')
  53.  
  54.             # Drop the table if it exists
  55.             drop_table(cursor, 'customers')
  56.  
  57.             # Create a new table
  58.             create_table(cursor, 'customers', 'id int primary key auto_increment, name varchar(20) not null')
  59.  
  60.             # Rename the table (optional)
  61.             # rename_table(cursor, 'customers', 'new_customers')
  62.  
  63.             # Create a new student
  64.             create_student(cursor, "Aung Win Htut", 20)
  65.  
  66.             # Read all students
  67.             print("All students:")
  68.             students = read_students(cursor)
  69.             for student in students:
  70.                 print(student)
  71.  
  72.             # Update a student
  73.             update_student(cursor, 1, "New Student", 25)
  74.  
  75.             # Read all students after update
  76.             print("\nAll students after update:")
  77.             students = read_students(cursor)
  78.             for student in students:
  79.                 print(student)
  80.  
  81.             # Delete a student
  82.             delete_student(cursor, 1)
  83.  
  84.             # Read all students after delete
  85.             print("\nAll students after delete:")
  86.             students = read_students(cursor)
  87.             for student in students:
  88.                 print(student)
  89.  
  90.         cnx.commit()
  91.         cnx.close()
  92.     else:
  93.         print("Could not connect to the database")
  94.  
  95. if __name__ == "__main__":
  96.     main()
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement