Python for the .NET developer Transcripts
Chapter: Course conclusion
Lecture: Databases and ORMs review
0:00 What would the Web be without databases?
0:02 So, of course, we talked about ORMs and then Python.
0:05 Probably the most popular ORM is SQLAlchemy.
0:08 We also have Django ORM, but as we discussed
0:10 it's tied specifically to Django.
0:12 SQLAlchemy works anywhere.
0:14 On the web, in Flask, Pyramid, or just in a standalone app.
0:18 So here's the basic thing we're going to do.
0:19 We're going to create classes that derive
0:21 from a SQLAlchemy base type that we dynamically create.
0:25 And these classes are going to map over to database tables.
0:29 We set up the columns, ID name, image style and price.
0:33 And we set the column type, like an integer or a string.
0:36 For the first one we say is a
0:38 auto-incrementing primary key.
0:39 That all happens automatically by just putting that here.
0:42 And then we also have the style
0:44 and the price set with an index
0:46 so that we can query them, query by them
0:48 or sort by them quick and easy.
0:51 Once we have these set up we can go
0:52 and just do queries with it.
0:54 We create this unit of work called a session.
0:56 Say session query of the type, filter.
0:59 And then we just use the static version of these values.
1:02 guitar.style == style.
1:04 And then get us all of 'em, boom!
1:06 That's it, that's the query where we go to the database
1:08 and then turn all of the whatever.
1:11 Let's say the style's electric, all of the electric guitars
1:13 into instances of our guitar class, all populated.
1:17 Works like a charm.