UPDATE data in the database
We write a function to update the data in our database and we do function call to execute the code under the function.
Below is the code to update the data in our database.
- We are creating another function called updateData(): that is going to update data in our database.
- As usual we use try: and except: block to try to update data in our database, if anything fails except block will catch that error and print it out for us.
- Our SQLite script is going to tell the interpreter that we want to UPDATE the STUDENTINFO table and the column we want to update in that table is age, which we want to be changed to 10 where the unique row ID is 1.
- As usual we are committing to it and printing it out if the update is successful, else our except block will catch that error and print it out for us.
- Finally we are calling out 2 functions here, one is to update the table and other is to print out the updated table which is selectData() function.
Below is the output of our above code when executed.
DELETE data in the database
We write another function to delete the data in our database and we do function call to execute the code under our function.
Below is the code to delete the data in our database.
- Here we are creating another function deleteData(): under which we have put our SQLite delete code.
- In the code we are specifying we want to delete particular ID from our STUDENTINFO table.
- And we are printing out our content before and after deleting data from our table by calling corresponding function.
- As you could see in the below output ID1 and its corresponding data are deleted.
In the next and final article, we will be doing a project that is going to get the user input from us, store it in the database which we could manipulate by performing various operations that we have learned.