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 set the connection string and some of the pulling settings and things like that,
0:11 so the next thing I'm going to do is I'm going to create this thing called a db factory or db session factory,
0:17 so in order to work with sqlalchemy, we work with this thing called a unit of work,
0:21 which is manifest in this session object in the sqlalchemy type system. So, we're going to want to basically configure this thing
0:28 and then we'll be able to ask it for sessions, so the next thing I want to add is something I'm going to call db session,
0:34 let's call it db factory, so we want to come over and create a class
0:38 and it's mostly going to be static, but we'll say db session factory, we'll go with this, and down here we're going to have a function
0:46 that does like overall setup for the system so I'll define a function called global init and we're going to call this one time
0:52 and it's not going to be a self, it's going to be a static method, a static method, class method, it doesn't matter
0:59 but it's not going to be an instance method. So we're going to come over here and we're going to do a couple of things,
1:04 the first thing that we want to do is we want to come up with the connection string, so the connection string of course varies by database
1:11 if we're talking to mysql, it might be a machine name and something like that; if we're talking to sqlite, with it's going to be file based
1:18 and we're going to use sqlite here because it's a no configuration database built in to Python, right
1:24 so it's super easy for you guys to just pick up this project and go, there's no like oh you forgot to set up the database
1:30 so we'll just go with this, with sqlite, in order to do that we need to have a location that we want to put these files into,
1:35 so let's do this and make like a data folder, call this db, just four source control so this thing gets created
1:42 I'm going to create a file here called placeholder.txt once we run it, it's going to basically put a database file right there.
1:50 So let's go back over here and we want to figure out that place so we're going to import OS, all right, and we're going to import our own package name
1:58 which is a restful auto service, okay so we're going to use this things to figure out the past, so we're going to say working folder= os.path.dirname
2:10 so this will give us the directory, if we give it a file to give us the directory that that's contained in,
2:16 what file are we going to give it, the root file for our package so that's going to give us this directory here,
2:21 and I somehow want to take that and combine db as a folder and some file name, so we'll say file, it's going to be equal to os.path.join
2:31 working folder db and let's just call it dealership.sqlite, the extension doesn't matter but I always like to put sqlite or something like that
2:43 to like go this is a sqlite file. Alright, so this is the actual file that we are going to work with,
2:48 and finally, we're going to get the connection string it's going to be sqlite:/// plus the file
2:56 so then we have our connection string and we're pretty much ready to go. So once you have a connection string
3:03 the next thing you have to work on is the engine.


Talk Python's Mastodon Michael Kennedy's Mastodon