00:00 As I've said before, we're using SQLite3
00:02 for our database for this app.
00:05 This here is how you actually open your connection
00:07 to the app and to the database
00:10 and then do something and close it off.
00:14 So we've got our connection.
00:16 We've covered this in SQLite3 before
00:18 but I'll give you a quick run through.
00:20 We connect to the database, okay?
00:22 We create the cursor which allows us
00:23 to actually write over the database.
00:25 We then execute some sort of code.
00:28 We commit it and then we close it.
00:31 Now as we know from our main menu,
00:33 there are quite a few things
00:34 that are going to need this connection, okay?
00:36 There's going to need, we need to add a room,
00:38 we need to view the room, we need to calculate totals,
00:41 we need to add inventory.
00:43 There's a lot of things there
00:44 that will need this connection.
00:46 Including just listing out the names of the rooms, right?
00:49 So you can't really have this much code
00:53 in a function every single time you need
00:56 to connect to the database.
00:59 It's not Pythonic and it's just a waste of code, right?
01:03 So what I've done is for this app,
01:05 I'll leave this up on the screen.
01:06 I've created a generator.
01:08 Let me paste it in here.
01:13 Alright, so this is called access_db, this function.
01:17 And it's wrapped in context manager,
01:19 so in order to use it, you would have
01:21 to from contextlib, import contextmanager
01:28 Whoops.
01:29 contextmanager, okay?
01:32 And what this is allows us to do is
01:33 it allows us to generate a cursor, okay?
01:37 So the first thing it's going to do, it's going to try.
01:39 It's going to connect to our database.
01:43 It then creates the cursor.
01:45 Again, we're using this lineup here, okay?
01:47 And then it's going to yield that cursor, okay?
01:50 And what does that mean?
01:51 It means it's going to pass back that cursor
01:55 to whatever called this function, okay?
01:58 So it yields this cursor out.
02:00 Now, just a quick demonstration
02:03 of what one of these functions looks like.
02:06 This is the list rooms function
02:09 that we saw previously, okay?
02:12 Now what it does is it says with access_db as cursor, okay?
02:18 So it's calling our access database function here.
02:25 And it's assigning that as cursor, okay?
02:29 So when it yields cursor, whatever is yielded
02:32 by this generator is assigned to cursor, okay?
02:37 So that way we're able to use this cursor here
02:39 that's generated by this try statement.
02:44 We're able to use it here and so it's going
02:46 to do cursor.execute and run our SQLite query.
02:52 And then once everything in this with statement is complete,
02:58 we move back up here into this,
03:01 into our generator, and we finish it off.
03:05 It's just finally, okay?
03:06 So this finally is dependent on
03:08 this yield coming back, okay?
03:11 So this returns a cursor, this yields a cursor
03:14 but this here, this function is generated
03:17 doesn't complete until whatever was
03:20 using this yielded cursor completes, okay?
03:25 So, yielded cursor into here.
03:27 Into this with statement.
03:29 This with statement uses the SQL.
03:32 Runs this quick for loop with this list
03:34 to create it and then once it's closed off,
03:39 we go back here.
03:41 We commit the change and then we close it.
03:44 And now you can see, we don't need to have
03:46 this specific code, the connection,
03:48 the cursor, the commit or the close.
03:51 We don't have to have that in every single function
03:55 that calls or that needs to talk to our database.
03:58 All we really want from this
04:01 database call, is the cursor.
04:04 This cursor is what's important
04:06 and that's what will change from function to function.
04:09 So by putting all of the unnecessary duplicate code
04:14 into its' own function up here,
04:16 we're being more Pythonic
04:18 and we're going to then only return
04:21 what we need and the yield what we need
04:23 to the functions that need to talk to the database.
04:26 Okay, so that's a quick, quick overview
04:29 of how we're using a generator
04:31 in this specific code base,
04:33 in this app to talk to our SQLite3 database.
