fork download
  1. import mysql.connector
  2.  
  3. # Connect to MySQL database
  4. conn = mysql.connector.connect(
  5. host="localhost",
  6. user="your_username",
  7. password="your_password",
  8. database="SELECTION_DB"
  9. )
  10.  
  11. # Create cursor
  12. cursor = conn.cursor()
  13.  
  14. # Create table EMP_SELECTION if not exists
  15. create_table_query = """
  16. CREATE TABLE IF NOT EXISTS EMP_SELECTION (
  17. FIRST_NAME VARCHAR(255),
  18. LAST_NAME VARCHAR(255),
  19. AGE INT,
  20. GENDER VARCHAR(1),
  21. INCOME DECIMAL(10, 2)
  22. )
  23. """
  24. cursor.execute(create_table_query)
  25.  
  26. # Add a column 'address' to the table EMP_SELECTION
  27. alter_table_query = """
  28. ALTER TABLE EMP_SELECTION
  29. ADD COLUMN address VARCHAR(255)
  30. """
  31. cursor.execute(alter_table_query)
  32.  
  33. # Insert a record into EMP_SELECTION table
  34. insert_query = """
  35. INSERT INTO EMP_SELECTION (FIRST_NAME, LAST_NAME, AGE, GENDER, INCOME, address)
  36. VALUES (%s, %s, %s, %s, %s, %s)
  37. """
  38. record = ("John", "Doe", 25, "M", 50000.00, "123 Street")
  39. cursor.execute(insert_query, record)
  40.  
  41. # Commit changes to the database
  42. conn.commit()
  43.  
  44. # Update records where GENDER is 'M' and increase AGE by one year
  45. update_query = """
  46. UPDATE EMP_SELECTION
  47. SET AGE = AGE + 1
  48. WHERE GENDER = 'M'
  49. """
  50. cursor.execute(update_query)
  51. conn.commit()
  52.  
  53. # Delete records where AGE is less than 18
  54. delete_query = """
  55. DELETE FROM EMP_SELECTION
  56. WHERE AGE < 18
  57. """
  58. cursor.execute(delete_query)
  59. conn.commit()
  60.  
  61. # Close cursor and connection
  62. cursor.close()
  63. conn.close()
  64.  
Success #stdin #stdout 0.02s 25936KB
stdin
Standard input is empty
stdout
import mysql.connector

# Connect to MySQL database
conn = mysql.connector.connect(
    host="localhost",
    user="your_username",
    password="your_password",
    database="SELECTION_DB"
)

# Create cursor
cursor = conn.cursor()

# Create table EMP_SELECTION if not exists
create_table_query = """
CREATE TABLE IF NOT EXISTS EMP_SELECTION (
    FIRST_NAME VARCHAR(255),
    LAST_NAME VARCHAR(255),
    AGE INT,
    GENDER VARCHAR(1),
    INCOME DECIMAL(10, 2)
)
"""
cursor.execute(create_table_query)

# Add a column 'address' to the table EMP_SELECTION
alter_table_query = """
ALTER TABLE EMP_SELECTION
ADD COLUMN address VARCHAR(255)
"""
cursor.execute(alter_table_query)

# Insert a record into EMP_SELECTION table
insert_query = """
INSERT INTO EMP_SELECTION (FIRST_NAME, LAST_NAME, AGE, GENDER, INCOME, address)
VALUES (%s, %s, %s, %s, %s, %s)
"""
record = ("John", "Doe", 25, "M", 50000.00, "123 Street")
cursor.execute(insert_query, record)

# Commit changes to the database
conn.commit()

# Update records where GENDER is 'M' and increase AGE by one year
update_query = """
UPDATE EMP_SELECTION
SET AGE = AGE + 1
WHERE GENDER = 'M'
"""
cursor.execute(update_query)
conn.commit()

# Delete records where AGE is less than 18
delete_query = """
DELETE FROM EMP_SELECTION
WHERE AGE < 18
"""
cursor.execute(delete_query)
conn.commit()

# Close cursor and connection
cursor.close()
conn.close()