Welcome. In this lecture, you're going to learn how to update a record in a database using Python and up psycho PG two module. So let's assume that we want jack Doe, which is one of our employees in the IT department to be reassigned to the to be reassigned to the logistics department. And as soon as this change happens inside our company, we want to update the database as well. Of course, it would be silly to delete the entire record and then add it back using a different value for the department column. So instead, we choose to update the record in place.
So in order to do that, let's see the SQL syntax. So we have update the name of the schema dot the name of the table, the set keyword, and we are setting the department column to the value of logistics where and this is a condition right here where last name equals though. So going back to our database, our application is going to identify the record in the database where the last name is, though. And for that record and that record only, it will update the value inside the department column from IIT to logistics. So let's test this in Python. Let's open up the Python interpreter once again.
Since the connection is closed, let's open it as we did before. Okay, the connection is successful. Now it's time to initialize the cursor. So cursor equals connection dot cursor. And now we have cursor dot execute, and in between the parentheses of execute, and also in between double quotes. Let's copy and paste this command right here.
Let's hit enter. Okay, now let's check our table and see if the change has been applied. No, it looks like jack doe is still In the IT department. Why is that? Well, simply because we forgot to commit or change. So connection that commit.
And also, let's close the connection as well. Okay, now let's check the table once again. And we can see that jack doe is now part of the logistics department in our database. Okay, so this is the way in which you can update certain records in your database in your schema, also using a condition to identify the particular record that you want to update. Okay, having that said, I'll see you in the next lecture, where we are going to learn how to delete records from our database using Python.