#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: Create the database!

Login or purchase this course to watch this video and the rest of the course contents.
0:00 And now, for the fun part of actually creating our database. We've defined it, we've defined the data
0:07 that's going to go in it, which was that user class. And now, we create it. When you're working with databases in this sort of an environment
0:16 you don't actually manage them from within your primary script as you may have done before with SQLite3 in your Flask apps or anything like that.
0:27 No, in this instance, we actually manage it on the command line or within the Python shell. Specifically here, we're going to pop into the Python shell
0:36 and we're going to actually create our database there fro the very first time. Now, it doesn't matter where you do this from.
0:44 If you do it within this current folder, the Project Awesome within this Flask view, it will not be able to create itself
0:52 because it can't call all of the dependencies and all of the files. The pathing just isn't there. So we need to actually run it from
1:02 our Flask.login folder up here, the parent folder. And by doing it from here, it will be able to talk to the init.py file and create our database
1:14 call on the right libraries and what-not. So launch your Python shell from your parent folder. Now, this is going to seem really simple
1:22 and it's because it it. We're actually just going to import that database object from that init.py file from our project.
1:33 And when we import this, we run create_all against it and what this will do is it will create our database based on the models that we defined
1:44 in the models.py file. So let's import that. So from Project Awesome, import db. And just ignore that message, this is something
1:57 you can look into later. It doesn't really affect us now, to be honest. And we can now run dp.create underscore all
2:06 and that will create our database with all the columns and everything that we need. So exit out of the shell. You'll see there's nothing here
2:15 so pop into Project Awesome and there is our site users.db file. What I'd like you to do now is open up your SQLite database browser that we installed
2:27 in the last video and then take a look at that file. So open that database in your program and you should see something like this. There we go.
2:38 We have a table, we have a single table and it is called user. Now, this is based on us creating that user model file.
2:49 So within our models file, we created a user class and we ended up with a user table here. If we click on the browse data tab
2:59 we then have those three columns that we defined. We have ID, we have username, and we have password.
3:07 Now when we use our actual Flask app, we will be able to see all of these get populated as data is being pumped in from the web


Talk Python's Mastodon Michael Kennedy's Mastodon