1
00:00:05,220 --> 00:00:09,780
Welcome back, everyone, to this lecture on updating existing entries in a database.

2
00:00:11,540 --> 00:00:17,960
Now, luckily for us, Gyngell makes it super easy to update an existing entry, only to do is grab

3
00:00:17,990 --> 00:00:24,050
the existing data entry, update any attributes by simply overwriting them just as you would any other

4
00:00:24,050 --> 00:00:26,270
attribute for a Python class object.

5
00:00:26,540 --> 00:00:31,820
And then you call Dot save on that object, and then those changes are going to write that update to

6
00:00:31,820 --> 00:00:32,780
the database table.

7
00:00:33,200 --> 00:00:35,030
Let's check this out in our code editor.

8
00:00:35,480 --> 00:00:35,750
All right.

9
00:00:35,750 --> 00:00:37,940
So here I am inside a visual studio code.

10
00:00:38,330 --> 00:00:44,360
Remember, previously we updated the heart rate as a new attribute and left a default value of 60.

11
00:00:44,900 --> 00:00:50,300
Now, let's imagine I wanted to grab a specific patient and maybe adjust their age.

12
00:00:50,300 --> 00:00:54,560
Or maybe they got married and they changed their last name or adjust their default heart rate.

13
00:00:54,590 --> 00:00:56,390
I would actually update that entry.

14
00:00:56,810 --> 00:01:03,020
Well, what I'm going to do first is actually begin the shell, so I'm going to call Python manage the

15
00:01:03,020 --> 00:01:04,280
pie shell.

16
00:01:05,560 --> 00:01:09,880
And now we have the shell that I can work with, keep in mind, is running an eye python, yours, maybe

17
00:01:09,880 --> 00:01:10,990
just running in bass python.

18
00:01:11,380 --> 00:01:17,380
But now that I have this, I should be able to actually import models and then begin to grab the patient

19
00:01:17,380 --> 00:01:17,760
model.

20
00:01:18,220 --> 00:01:21,340
So I'm going to say from office models.

21
00:01:22,380 --> 00:01:24,390
Import patient.

22
00:01:25,610 --> 00:01:32,450
And then how do I grab a particular patient recall, I can say something like patient objects, and

23
00:01:32,450 --> 00:01:36,840
then maybe I want to get one where the primary key is equal to one.

24
00:01:36,860 --> 00:01:41,480
So just the very first patient and that returns Carl Smith, who's 30 years old.

25
00:01:41,960 --> 00:01:44,600
Let's say Carl decided to change their last name.

26
00:01:45,020 --> 00:01:48,650
How would actually update their entry in my actual database?

27
00:01:49,010 --> 00:01:55,730
Well, I would first need to assign this actual instance of the object so I can say something like Carl

28
00:01:56,360 --> 00:02:03,320
is equal to and then again patient that objects that get primary keys equal to one.

29
00:02:04,100 --> 00:02:08,990
Obviously, there's many different ways I could either filter or get a list of entries I wanted to update,

30
00:02:08,990 --> 00:02:11,720
etc. just doing it right now with a single entry.

31
00:02:12,110 --> 00:02:18,770
And then what I can do after this is check out Carl, who again is the patient and I can check out Carl's.

32
00:02:19,960 --> 00:02:20,650
Last name.

33
00:02:22,050 --> 00:02:27,180
And it's Smith, let's say I wanted to update Karl last name, remember, Karl hears just the variable

34
00:02:27,180 --> 00:02:28,400
I could call this person.

35
00:02:28,410 --> 00:02:36,150
It doesn't really have to be their first name, so I can say Karl last name and then simply reassign

36
00:02:36,150 --> 00:02:36,270
it.

37
00:02:36,600 --> 00:02:41,430
So maybe we are operating something like, again, a patient doctor's office.

38
00:02:41,430 --> 00:02:43,200
Someone gets married, they change the last name.

39
00:02:43,590 --> 00:02:47,670
And so now let's go ahead and change their last name to, I don't know.

40
00:02:47,670 --> 00:02:50,070
Let's say they call it jingo.

41
00:02:51,740 --> 00:02:59,210
So now Karl's last name is equal to jingo, so if I call Carl notice it says Carl Jingo is 30 years

42
00:02:59,210 --> 00:02:59,600
old.

43
00:02:59,780 --> 00:03:03,380
Remember, we were pointing out the last name first, according to this string entry.

44
00:03:03,830 --> 00:03:05,870
Now, this hasn't actually changed anything.

45
00:03:05,870 --> 00:03:09,980
We've only changed this object that we've been playing around with.

46
00:03:10,190 --> 00:03:17,690
However, this object does have information of what primary key it's in regards to the database table.

47
00:03:18,050 --> 00:03:25,070
So if I want to save this change of Carl's last name being jingo now, I simply need to call Carl Dot

48
00:03:25,070 --> 00:03:26,630
Save Enter.

49
00:03:26,660 --> 00:03:29,570
And that actually writes the change to the database.

50
00:03:29,600 --> 00:03:30,650
In fact, let's check this out.

51
00:03:31,130 --> 00:03:32,150
Make clear this.

52
00:03:33,160 --> 00:03:40,000
And I'm going to say patient the objects, and let's just grab all of them, so when I say patients,

53
00:03:40,000 --> 00:03:41,350
the objects all.

54
00:03:42,310 --> 00:03:45,410
Now, you see the very first patient here is General Carl.

55
00:03:45,920 --> 00:03:51,170
So it's pretty incredible that general gives you the flexibility to just grab any entry in that database,

56
00:03:51,650 --> 00:03:56,270
reassign those attributes just as you would if any other Python object and then you want to save that

57
00:03:56,270 --> 00:03:57,530
change or right, it's a database.

58
00:03:57,770 --> 00:03:59,330
You simply call that safe.

59
00:03:59,630 --> 00:04:05,600
That's going to give us a ton of flexibility inside of views that pie to actually interact with the

60
00:04:05,600 --> 00:04:07,070
data, which we'll see later on.

61
00:04:07,580 --> 00:04:10,850
But that's pretty much it for updating an entry in a database.

62
00:04:11,240 --> 00:04:11,840
Come up next.

63
00:04:11,840 --> 00:04:14,180
Let's talk about removing items or deleting them.

64
00:04:14,390 --> 00:04:14,930
I'll see you there.

