Advertisement
DrAungWinHtut

pymysqldbfunctions.py

Feb 21st, 2023
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. # Function to create a connection to the database
  2. def connect_to_database(db_name):
  3.     try:
  4.         conn = pymysql.connect(host='localhost',
  5.                                user='root',
  6.                                password='',
  7.                                database=db_name)
  8.         print("Connected to the database successfully")
  9.         return conn
  10.     except Exception as e:
  11.         print("Error connecting to the database: ", e)
  12.  
  13. # Function to create a new database
  14.  
  15.  
  16. def create_database(conn, db_name):
  17.     try:
  18.         cursor = conn.cursor()
  19.         cursor.execute("CREATE DATABASE %s", (db_name,))
  20.         conn.commit()
  21.         print("Database created successfully")
  22.     except Exception as e:
  23.         conn.rollback()
  24.         print("Error creating database: ", e)
  25.  
  26. # Function to drop an existing database
  27.  
  28.  
  29. def drop_database(conn, db_name):
  30.     try:
  31.         cursor = conn.cursor()
  32.         cursor.execute("DROP DATABASE %s", (db_name,))
  33.         conn.commit()
  34.         print("Database dropped successfully")
  35.     except Exception as e:
  36.         conn.rollback()
  37.         print("Error dropping database: ", e)
  38.  
  39. # Function to create a new table
  40.  
  41.  
  42. def create_table(conn, table_name, columns):
  43.     try:
  44.         cursor = conn.cursor()
  45.         cursor.execute("CREATE TABLE %s (%s)", (table_name, columns))
  46.         conn.commit()
  47.         print("Table created successfully")
  48.     except Exception as e:
  49.         conn.rollback()
  50.         print("Error creating table: ", e)
  51.  
  52. # Function to drop an existing table
  53.  
  54.  
  55. def drop_table(conn, table_name):
  56.     try:
  57.         cursor = conn.cursor()
  58.         cursor.execute("DROP TABLE %s", (table_name,))
  59.         conn.commit()
  60.         print("Table dropped successfully")
  61.     except Exception as e:
  62.         conn.rollback()
  63.         print("Error dropping table: ", e)
  64.  
  65. # Function to insert data into a table
  66.  
  67.  
  68. def insert_data(conn, table_name, values):
  69.     try:
  70.         cursor = conn.cursor()
  71.         query = "INSERT INTO %s VALUES (%s)"
  72.         cursor.executemany(query, [(table_name, value) for value in values])
  73.         conn.commit()
  74.         print("Data inserted successfully")
  75.     except Exception as e:
  76.         conn.rollback()
  77.         print("Error inserting data: ", e)
  78.  
  79.  
  80. # Function to select data from a table
  81. def select_data(conn, table_name, columns="*", condition=None):
  82.     try:
  83.         cursor = conn.cursor()
  84.         if condition is None:
  85.             query = "SELECT %s FROM %s"
  86.             cursor.execute(query, (columns, table_name))
  87.         else:
  88.             query = "SELECT %s FROM %s WHERE %s"
  89.             cursor.execute(query, (columns, table_name, condition))
  90.         result = cursor.fetchall()
  91.         for row in result:
  92.             print(row)
  93.     except Exception as e:
  94.         conn.rollback()
  95.         print("Error selecting data: ", e)
  96.  
  97. # Function to alter an existing table
  98.  
  99.  
  100. def alter_table(conn, table_name, action):
  101.     try:
  102.         cursor = conn.cursor()
  103.         query = "ALTER TABLE %s %s"
  104.         cursor.execute(query, (table_name, action))
  105.         conn.commit()
  106.         print("Table altered successfully")
  107.     except Exception as e:
  108.         conn.rollback()
  109.         print("Error altering table: ", e)
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement