Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Using our database with the SQLAlchemy ORM
Lecture: Connecting to the database

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, So let's go ahead and call from our app over here.
0:05 Certainly not like just piling all this code here.
0:07 So let's create a function called Configure and then we can configure the database,
0:11 configure the routes and so on.
0:18 So let's just move some stuff into here,
0:21 which is def business. Now I have configured_db().
0:32 I'm going to put that to the side for a minute.
0:33 Now, down here, if it's been run directly,
0:37 which is maybe in development, depending how you run it,
0:40 we want to make sure we call configure.
0:42 But in production, we don't want to call run.
0:44 We just want to call configure.
0:46 I don't want to do this step,
0:47 so I'm going to make sure we call it either way.
0:51 Alright. Final thing to do is just come up with a database
0:55 folder name, or file name that we want to work with.
0:58 So what I want to do is I want to go to this file wherever app
1:02 is, and I want to make sure we have a folder over here.
1:06 I'll call it SQL and inside sql.
1:09 We're going to have our database file gonna live in this directory and not necessarily going
1:14 to check it into the database and to GitHub.
1:17 But it's going to go into here and be automatically created by SQLAlchemy.
1:22 What's a good way to do that?
1:23 Well, we can come up here and say from pathlib,
1:27 import the path, class. This is one of the newer ways to work with past
1:31 not super new but newer than some of the older styles in I thought to work
1:36 with paths in a clean way.
1:38 What we can say is that the
1:40 db_file is going to be create a path.
1:43 Give it the dunder file(__file__) that's this app.
1:46 File the full path on the hard drive to this file,
1:50 this module and then we can say,
1:52 Parent, that will go from here to here.
1:56 And then there's an interesting way where they overload the divide operator so you can see
2:00 it is to be SQL,Yeah,
2:02 and then in the sql, what do we want?
2:04 The file name to be cloud city.SQLLite.
2:09 All right, that's it. That's all we should have to use.
2:12 So we come over here safe session,
2:15 import db_session and say global,
2:17 in it? db_file. You gotta be careful here.
2:21 It's easy to pass this along.
2:22 We look over here, this expects a string.
2:25 This is not actually a string.
2:27 It's a path object. We can say as posix() as a URL.
2:33 And we'll get that as a the right kind of string.
2:35 Or we could just call string on it.
2:38 However you get it, we've got to make sure you convert this to a string
2:40 when we pass it over. Well,
2:42 let's go and run it and see what happens.
2:47 Oh, yes. Uh, one other thing we need to set here notice,
2:50 We got to specify a table name in our order.
2:56 We also need to do this for our other ones.
2:57 So let's let's do that. Now.
3:00 This is going to tell SQLalchemy what we want.
3:02 The actual table name, Not just the column
3:05 Names in the database to be, I want to use orders.
3:09 lowercase and plural. That's just the way I like to do things that
3:12 will do the same thing for our user, because we have exactly the same problem.
3:19 This won't be users. Try one more time.
3:23 All right. Well, it looks like it ran.
3:25 And you can see it's connecting to the quote db with this full path right here
3:30 which Yeah, that looks correct.
3:32 If we look in here and we reload from disk to look at this,
3:37 we have a cloudcity.sqlite database.
3:41 Super cool. Let's just see if we can see what's in it.
3:44 Normally I could come over here and add data source from sqlite.
3:50 I don't think this is gonna work.
3:52 I'll give it a quick try.
3:53 Notice. I can download the missing drivers.
3:57 We can go to the file.
4:02 Choose that right there. Test connection,
4:06 oh! it worked. What I was going to say is till very recently,
4:10 a few weeks ago, this didn't work because this is an apple M1,
4:14 the new arms silicon version. And they didn't have drivers that supported Apple silicon.
4:19 It only was intel. So look at this.
4:20 All right, Now let's see what we got.
4:22 We go over to our schemas and expand this out.
4:24 We've got our orders, which has?
4:26 id, created a datetime notice. They have indexes.
4:29 Those are the blue, and then the primary key.
4:32 Here's another index. Um, actually listed there.
4:35 Here's our users as well. Awesome.
4:38 So we've used sqlalchemy to define some classes to store in our database and then
4:42 SQLAlchemy is actually connected to SQLLite and then created the database structure that we're
4:48 going to need to work with those classes in that database.
4:52 Fantastic. I just love sqlalchemy.
4:54 It's so easy and nice to work with.
4:56 If you're doing a relational databases,definitely recommended it.