Monday, February 18, 2019

Updating and Deleting Records From the Database Using Python

I have previously created blog articles how to insert a record into our database and  how to read database records using Python. Now, it is time to update and / or delete records from our database.

When we update, we can ask our user for a record to update, search it in our database and once this record is found, we retrieve it for updating.

The sample Python program code below shows how we can update the age of a certain person using the fname(first name) as our search condition (see highlighted text below).

You can change the age as a user input to make your code more dynamic.

Now, if you need to delete a record, we follow the same process we did with updating but we change our SQL command to enforce record deletion.

In the Python program code below, we ask the user for a first name to locate and delete, once we found the record, the SQL Delete command shall be executed.

You may need to refresh your database table to see effect.

Now, it is your turn to practice these modules on your own database!