In Python, To Delete a record from a MySQL table using Python, First, we need to install "pip" if haven't installed it already.
Python provides the "mysql-connector-python" library to connect the database with the python application.
Below is an example demonstrating how to delete a record from a table:
After installing the package, we can use it to establish a connection to our MySQL database and perform operations.
pip install mysql-connector-python
Import `mysql.connector` library and create a configuration object with keys such as `user`, `password`, `host`, and `database`.
# import mysql connector library import mysql.connector # Configuration parameters config = { 'user': 'your_username', 'password': 'your_password', 'host': 'localhost', # or your host address 'database': 'your_database_name', }
Delete Record from Table using the `DELETE` command and with the help execute() method we can run the operation to delete records from the table in MySQL database.
# Define the delete query delete_query = "DELETE FROM your_table_name WHERE id = %s" # ID of the record to delete record_id_to_delete = 1 # Change `ID` of the record you want to delete # Execute the delete query cursor.execute(delete_query, (record_id_to_delete,)) # Commit the transaction connection.commit() print("Record deleted successfully")
Don't forget to close the connection once done using the "close()" method.
finally: if 'connection' in locals() and connection.is_connected(): cursor.close() connection.close() print("MySQL connection is closed")
import mysql.connector # Configuration parameters config = { 'user': 'your_username', 'password': 'your_password', 'host': 'localhost', # or your host address 'database': 'your_database_name', } try: # Establish a connection to the MySQL server connection = mysql.connector.connect(**config) if connection.is_connected(): # Create a cursor object to execute SQL queries cursor = connection.cursor() # Define the delete query delete_query = "DELETE FROM your_table_name WHERE id = %s" # ID of the record to delete record_id_to_delete = 1 # Change `ID` of the record you want to delete # Execute the delete query cursor.execute(delete_query, (record_id_to_delete,)) # Commit the transaction connection.commit() print("Record deleted successfully") except mysql.connector.Error as error: print("Error occurred while connecting to MySQL:", error) finally: if 'connection' in locals() and connection.is_connected(): cursor.close() connection.close() print("MySQL connection is closed")
Make sure to replace 'your_username', 'your_password', and 'your_database_name' with your actual MySQL credentials and database name.
Replace 'your_table_name' with the name of the table you want to delete records from.
This script connects to the MySQL server, deletes the record with the specified ID from the specified table, and prints a success message if the operation is successful.
Remember to handle exceptions and close the connection properly. Also, ensure you specify the correct record ID to delete.