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


Talk Python's Mastodon Michael Kennedy's Mastodon