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