#100DaysOfWeb in Python Transcripts
Chapter: Days 33-36: Database access with SQLAlchemy
Lecture: Setting up our app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here we are in our GitHub repository for this chapter. Let's get our app set up. We're going to start from just a little bit of code
0:07 so we stay focused on the database sides of things instead of all the user interaction stuff that I'm going to write.
0:12 Let's go over here to the starter. It's empty right now. I'm going to set up a little bit of this for you
0:18 and then make a snapshot so if you want to pick up from there and keep going, that'd be great. So we're going to go over to that folder
0:24 and as always create a virtual environment for installing third-party packages let's stay focused on just the ones we need. Should be good.
0:38 Now we're going to need a couple of utility libraries to get started, so I'm just going to put those in here.
0:43 So over here we have this switch statement thing that I'm going to use and this thing for parsing numbers a little more nicely and the ability
0:51 to locate our database folder, we're going to use SQLite which means we're just going to have a file living here, we're not using what you might call
0:58 a proper database server. What we're doing here will exactly work on things like Postgres and MySQL and so on. It just so happens it's easier for you
1:08 to pick up this project and use SQLite 'cause I don't have to give you lots of instructions on how to set up the database
1:14 how to set it up so it's at the right location so it has the right permissions. You can just run it. So we're going to use SQLite 'cause it's simple
1:21 but what you learn here will apply to all the databases. And finally, because we're using SQLite we added that as a requirement, so let's go ahead
1:28 and install that. Okay, looks like it's all good for the foundations and then the last thing I want to do is add
1:38 the actual program that we're going to write and I'll call it program just so it's clear what we're going to run and go ahead and run it
1:44 to create a run configuration up here. Obviously it does nothing. But, I do want to get the structure in place. Let's talk about that really quick.
1:53 So, over here, we're going to have a main method it's going to set up the database and then it's going to go through this loop
2:00 over and over asking for a command and then using my little switch statement that I created. Get this in GitHub if you care.
2:07 Or you obviously have it here as well. It'll say if they typed in r, run the method. Rent a scooter. If they typed in a, run the method.
2:15 Find available scooters. If they hit x or nothing, exit the app, things like that. And it's just going to go along and run
2:23 these various methods, none of which have any implementation other than just a little bit of printout and even a cool little misspelling there.
2:31 So, we're going to run this. And I'll just show you the interaction. There's zero database stuff going on right?
2:37 This is just a little user interaction here so we could say I'd like to locate my scooters. It says, here's your scooters.
2:43 I'd like to see my personal history. It says here's your rental history. I could say boo it says I don't know what to do with Boo
2:48 I could hit X and we're done. So this is where we're starting from. We have this structure of the sort of interactive command line interface.
2:58 And then we have these methods that are going to do the database thing. Now, one thing you might notice is this is not a web app.
3:04 We're talking about databases because databases are the foundation of web applications. However, I wanted to start
3:12 just from the pure data access side of things. So we're going to do it in a command line interface that has nothing to do with HTML and CSS and views
3:20 and forms and all of that. We're just going to do the simple command line interface here and then part of your project may be to
3:27 adapt it to the web, but once you learn how to use SQLAlchemy, it doesn't matter if it's a web app or not. Right, now you just write the same code.
3:36 So this lets us focus on just the data access side of things.

Talk Python's Mastodon Michael Kennedy's Mastodon