Anvil: Web apps with nothing but Python Transcripts
Chapter: Databases: Storing and querying data
Lecture: Saving a measurement to the database

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we have our server site module working
0:02 let's do this little silly example way
0:06 and let's make something that we actually care about.
0:08 What we want to do is, we want to save our measurements.
0:11 So let's call this add_measurement
0:16 and let's think about the things we're going to need
0:18 from our database.
0:22 Over here, we're going to need a user
0:25 that's fine, actually the user
0:27 is passed in a side channel
0:28 you don't actually have to pass user
0:30 it's kind of like you can always just ask
0:32 for who is the logged in user
0:34 either on the client or the server
0:35 so that'll be easy and then we have
0:37 record date, created date, weight and pounds
0:40 and resting heart rate.
0:46 Now, the created date, you think the server
0:48 knows what time it is?
0:50 Yes, so we don't actually have to ask that.
0:52 And come up here and say import datetime
0:57 and down here, we just say and you have
0:59 a standard python
1:02 like that date, datetime.now
1:04 So that works pretty well, now what are we going to do?
1:07 We have to create one of these records and save it.
1:10 But we want to make sure that when it's called
1:12 the user's logged in.
1:13 So let's do this.
1:14 Say user remember this anvil.users.getuser?
1:18 Oh, that works the same here and when you say
1:21 if not user, then we can either raise an exception
1:24 or just return some kind of error.
1:31 Now of course our app is going to enforce that
1:34 we're logged in, in order to call this method
1:37 but you know just in case we make a mistake
1:39 or there's some flaw in our logic on the client side
1:42 well here we can double catch that.
1:44 And then it's super easy
1:45 we just go app_tables.measurements
1:48 and notice the auto complete right here
1:50 super nice.
1:52 Say add_row and look what it shows us right there.
1:54 Going to say user, weight in pounds, record date
1:57 created date and so on
2:00 Let's say created date equals created date
2:04 record date equals record date
2:08 resting heart rate
2:15 user equals user
2:16 and I notice I have an e right here
2:20 oh that should be a rate
2:21 so let's fix that real quick, okay
2:23 well, it looks like we pretty much have this
2:25 all ready to go.
2:27 The only thing is, to call it.
2:30 Well, let's go try that.
2:31 Now we've have a quick thing to fix real quick here
2:34 Remember, we put this little print here
2:35 so let's take that out, this doesn't exist anymore
2:38 it's going to cause a crash.
2:39 Here's where it gets interesting.
2:41 We said we would have recorded it
2:43 well let's go do it.
2:44 anvil.server.call
2:49 Here's our add_measurement
2:51 and what do we have to pass it?
2:53 Now, this dialogue here is in the way of that
2:57 which is annoying.
2:59 Let's see if we can make that go away
3:02 like so, maybe. There we go.
3:06 So we can pass in the record date
3:08 which is going to be self.date
3:11 we pass in the weight and pounds
3:13 self.weight
3:16 and we have self.rate, wow those are weird
3:20 A weird combination.
3:21 And let's just comment this out.
3:25 Alright, if everything's hanging together
3:28 when we go back here, we should now have at least
3:30 one entry in our table. Let's go.
3:33 Alright, we're logged in, we're going to go over here
3:35 and let's say the date is
3:37 let's do one for yesterday.
3:38 Yesterday was 179, my resting heart rate was 71.
3:44 Okay, that--did it work?
3:46 I don't know, let's try to add one more.
3:48 Let's say now it's today and my rest
3:51 my weight is 177, and my resting heart is 69.
3:54 Maybe I've been gettin' in shape, I don't know.
3:57 Did it again, stop, let's go back to our tables
4:01 How about that? That is so cool, there is is!
4:04 We've got our recorded date, our record date
4:06 these are the days that we've entered
4:08 Here's the actual down to the millisecond time
4:11 when we made these actions
4:14 here's the weight in pounds that we typed in
4:16 here's the resting heart rate
4:19 and check this out
4:20 we actually have the user over here
4:23 that we could go to.
4:24 So it's, pull this up, you can see all the details
4:27 about the user and so on.
4:29 Isn't that cool?
4:30 Here's our foreign key relationship
4:32 and all we had to do to make that happen
4:35 was to call get current user
4:38 and then right there just pass it over.
4:41 Working with databases in Anvil is super easy
4:44 we didn't even have to know
4:46 the sequel query language or
4:47 third normal forum or any of those types of things
4:50 although it wouldn't be terrible to know.
4:52 It is a simple way to get started
4:54 and just connect these things right together.