RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Adding a database backend with SQLAlchemy
Lecture: Database sessions

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The next thing we're going to need to do is do some set up code
0:05 set the connection string and some of the pulling settings and things like that,
0:10 so the next thing I'm going to do is I'm going to create
0:13 this thing called a db factory or db session factory,
0:16 so in order to work with sqlalchemy, we work with this thing called a unit of work,
0:20 which is manifest in this session object in the sqlalchemy type system.
0:24 So, we're going to want to basically configure this thing
0:27 and then we'll be able to ask it for sessions,
0:30 so the next thing I want to add is something I'm going to call db session,
0:33 let's call it db factory, so we want to come over and create a class
0:37 and it's mostly going to be static, but we'll say db session factory, we'll go with this,
0:42 and down here we're going to have a function
0:45 that does like overall setup for the system
0:47 so I'll define a function called global init and we're going to call this one time
0:51 and it's not going to be a self, it's going to be a static method,
0:55 a static method, class method, it doesn't matter
0:58 but it's not going to be an instance method.
1:00 So we're going to come over here and we're going to do a couple of things,
1:03 the first thing that we want to do is we want to come up with the connection string,
1:06 so the connection string of course varies by database
1:10 if we're talking to mysql, it might be a machine name and something like that;
1:14 if we're talking to sqlite, with it's going to be file based
1:17 and we're going to use sqlite here
1:20 because it's a no configuration database built in to python, right
1:23 so it's super easy for you guys to just pick up this project and go,
1:26 there's no like oh you forgot to set up the database
1:29 so we'll just go with this, with sqlite, in order to do that
1:32 we need to have a location that we want to put these files into,
1:34 so let's do this and make like a data folder, call this db,
1:37 just four source control so this thing gets created
1:41 I'm going to create a file here called placeholder.txt
1:44 once we run it, it's going to basically put a database file right there.
1:49 So let's go back over here and we want to figure out that place
1:52 so we're going to import OS, all right,
1:55 and we're going to import our own package name
1:57 which is a restful auto service, okay
2:00 so we're going to use this things to figure out the past,
2:03 so we're going to say working folder= os.path.dirname
2:09 so this will give us the directory, if we give it a file
2:13 to give us the directory that that's contained in,
2:15 what file are we going to give it, the root file for our package
2:18 so that's going to give us this directory here,
2:20 and I somehow want to take that and combine db as a folder
2:24 and some file name, so we'll say file, it's going to be equal to os.path.join
2:30 working folder db and let's just call it dealership.sqlite,
2:39 the extension doesn't matter but I always like to put sqlite or something like that
2:42 to like go this is a sqlite file.
2:45 Alright, so this is the actual file that we are going to work with,
2:47 and finally, we're going to get the connection string
2:50 it's going to be sqlite:/// plus the file
2:55 so then we have our connection string and we're pretty much ready to go.
2:59 So once you have a connection string
3:02 the next thing you have to work on is the engine.