Mysql Delete in Python
MySQL Delete in Python
In Python, you can delete records or even an entire table from a MySQL database using the mysql-connector-python library.
1. Install MySQL Connector
If you haven’t installed the MySQL connector, install it using:
pip install mysql-connector-python2. Connect to MySQL Database
Before deleting data, establish a connection to your MySQL database.
import mysql.connector# Connect to MySQL Databaseconn = mysql.connector.connect( host="localhost", user="root", password="password", database="mydatabase" # Change to your database name)# Create a cursor objectcursor = conn.cursor()print("Connected to MySQL database successfully!")3. Delete a Specific Record
To delete a specific record, use the DELETE FROM SQL statement.
# SQL query to delete a specific recorddelete_query = "DELETE FROM employees WHERE id = %s"values = (3,) # Change ID to the record you want to deletecursor.execute(delete_query, values)conn.commit() # Commit changes to the databaseprint(cursor.rowcount, "record(s) deleted.")Explanation:
The
DELETE FROM employees WHERE id = %sdeletes an employee with ID3.cursor.execute(delete_query, values)executes the deletion.conn.commit()saves the changes to the database.cursor.rowcountshows how many records were deleted.
4. Delete Multiple Records
To delete multiple records based on a condition:
delete_query = "DELETE FROM employees WHERE department = %s"values = ("Sales",) # Delete all employees in the 'Sales' departmentcursor.execute(delete_query, values)conn.commit()print(cursor.rowcount, "record(s) deleted.")5. Delete All Records from a Table
To delete all records but keep the table structure:
cursor.execute("DELETE FROM employees")conn.commit()print("All records deleted from 'employees' table.")? Warning: This will remove all records from the table but keep the table itself.
6. Delete a Table
If you want to delete the entire table:
cursor.execute("DROP TABLE employees")print("Table 'employees' deleted successfully.")? Warning: This will delete the entire table permanently.
7. Closing the Connection
After performing deletions, always close the connection.
cursor.close()conn.close()print("MySQL connection closed.")Final Complete Code
import mysql.connector# Connect to MySQL Databaseconn = mysql.connector.connect( host="localhost", user="root", password="password", database="mydatabase")cursor = conn.cursor()# Delete a specific recorddelete_query = "DELETE FROM employees WHERE id = %s"values = (3,) # Change ID to the record you want to deletecursor.execute(delete_query, values)conn.commit()print(cursor.rowcount, "record(s) deleted.")# Close Connectioncursor.close()conn.close()Summary
| Task | SQL Query |
|---|---|
| Delete a specific record | DELETE FROM employees WHERE id = %s |
| Delete multiple records | DELETE FROM employees WHERE department = %s |
| Delete all records | DELETE FROM employees |
| Delete a table | DROP TABLE employees |
This is how you can delete records and tables in MySQL using Python! ?