Hi, welcome to this lecture. In this lecture, we are going to talk a bit about the commit and rollback methods. As we've seen so far, the commit method commits or saves the changes that have been applied since the last commit during the current database transaction. If you don't use commit, after applying some changes, these changes won't be visible from other connections to the database and won't be applied to your tables. On the other hand, the rollback method rolls back any changes that you've made to the database since the last time you used the commit method. So let me prove this to you.
I'm going to first import psycho PG two, then I'm going to establish a new connection to the database. And of course, initialize the cursor and looking at our database at this point. We currently have these five records that we previously used In this section thus far, Okay, now let's assume that we are inserting a new record into the database into the table. So using the cursor, I'm going to execute this SQL statement right here, insert into my staff that employees and I'm going to insert the sixth record right here, Gene centers, department, HR, phone number, address, and salary. And let me hit enter. Okay, at this point, we haven't committed this change yet.
So let's go over to this connection to the database right here, and query the table once again. And you can see that the new record hasn't yet been added to the table. On the other hand, if we return to this session, this connection to the database, let's use cursor dot execute in order to query our table from within this session, so select all from my staff dot employees. Next we have cursor dot fetch all and let's use the same variable records and for i in Records brain tie. So you can see that in this session in this connection to the database, our change, our new record has been added to the table. However, the change is not yet visible from this connection right here.
Okay, now what if we change our mind and we want to roll back this change and do not add Jane Sanders to our table. In order to do that, we are going to use connection dot rollback to roll back the changes since the last commit. And now if we perform the query once again, let's fetch all the records as well. So for i in records, print time, and you can see that after using the rollback method, the newly created record, Jane Sanders is not present in the database anymore. Okay, so this is the way in which rollback can be used. However, if we do want to insert this record into the table and make it visible to other connections, as well as saved to the database.
Let's use cursor dot execute once again. And now let's use connection dot commit. And at this point, this new record should be visible inside the database from other connections as well. And let's try this. And you can see this record being added right here, Jen Sanders and all the data associated with this record. Okay, so this is the way in which the roll back and commit methods work inside databases.
And I think the rollback method is pretty useful whenever you make some changes to the database and then change your mind. So having that said, I'll see you in the next lecture.