#100DaysOfWeb in Python Transcripts
Chapter: Days 33-36: Database access with SQLAlchemy
Lecture: The database is created
0:00 Lets look at our main now.
0:01 So heres our main method in our program
0:03 and this stuff UI loop
0:05 if you can call it that.
0:06 It goes around and around
0:07 and asks the user what they want
0:08 but first thing we have to do is setup the DB.
0:10 Now I've thrown in a few little to-dos
0:13 to remind me what this means in the right order.
0:15 So what we are going to do
0:16 is we're actually going to have to
0:18 initialize the connection by calling global_init.
0:21 Make sure the table schema is there
0:24 call it a second time
0:25 SQLAlchemy will just verify
0:26 that things are set up okay.
0:29 And then we actually want to import some data eventually
0:32 and set what's called the default user.
0:34 That will basically be whoever is logged in.
0:37 So lets go over here and say, session_factory
0:41 and import that at the top and say global_init
0:43 and what do we call it
0:48 called DB SQLite
0:49 name doesn't actually matter.
0:51 This line should actually
0:53 have the database created right here.
0:54 You'll see it appear and that's a folder
0:57 but it will have no schema.
0:58 It'll have, just be an empty database okay?
1:01 The next thing we need to do is go to session_factory
1:04 and say create_tables.
1:06 Then I'm going to leave these other two things here
1:08 because we are not ready for those yet.
1:10 Let's run this and see what happens.
1:14 Well this is already a good sign
1:15 the fact that it did that without crashing
1:17 that's like hurray.
1:19 Now if we look over here we now have this thing popping up
1:23 and it has a little database icon
1:25 see that blue icon?
1:27 That didn't come from the file extension
1:28 that came from PyCharm looking at
1:30 and going Ah-ha I understand what's in that file
1:33 it is a database a SQLite database, Professional
1:38 Community Edition this doesn't work for
1:39 so professional we go over here to the database
1:42 we can drop this here
1:43 and we can open it up and look at it.
1:45 Now notice it didn't really work
1:47 hmm why didn't it work? Lets hit this.
1:50 It says the driver is SQLite except for
1:52 there are no drivers for it
1:54 so you got to download them
1:55 wait a moment. Now we can click test
1:59 success. Go okay hit the little refresh
2:04 now we have a schema. We open up our schema
2:06 we have a main and look at that
2:08 locations, rentals, scooters what's in here?
2:10 Open it up, oh it has an id
2:11 which you can see there's a primary key
2:14 these yellow vertical things mean those have indexes
2:18 all sorts of good stuff.
2:19 And this is an integer and this is a varchar
2:21 and things like that. This is the datetime
2:24 so it looks like SQLAlchemy has properly created our
2:27 database schema based on our classes.
2:30 Yay! Put that away so that part is pretty much up and running.