Python for .NET Developers Transcripts
Chapter: Course conclusion
Lecture: Databases and ORMs review
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
What would the Web be without databases? So, of course, we talked about ORMs and then Python. Probably the most popular ORM is SQLAlchemy.
0:09
We also have Django ORM, but as we discussed it's tied specifically to Django. SQLAlchemy works anywhere.
0:15
On the web, in Flask, Pyramid, or just in a standalone app. So here's the basic thing we're going to do. We're going to create classes that derive
0:22
from a SQLAlchemy base type that we dynamically create. And these classes are going to map over to database tables.
0:30
We set up the columns, ID name, image style and price. And we set the column type, like an integer or a string. For the first one we say is a
0:39
auto-incrementing primary key. That all happens automatically by just putting that here. And then we also have the style
0:45
and the price set with an index so that we can query them, query by them or sort by them quick and easy. Once we have these set up we can go
0:53
and just do queries with it. We create this unit of work called a session. Say session query of the type, filter.
1:00
And then we just use the static version of these values. guitar.style == style. And then get us all of 'em, boom!
1:07
That's it, that's the query where we go to the database and then turn all of the whatever. Let's say the style's electric, all of the electric guitars
1:14
into instances of our guitar class, all populated. Works like a charm.