Python for .NET Developers Transcripts
Chapter: Database access and ORMs in Python
Lecture: SQL query equivalents
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's round out this chapter on SQLAlchemy by showing you some more advanced query syntax. It was pretty cool that we could say
0:08
guitar.style == style, and that was great. But when it gets more complicated how do you do that, right? How do I do, like, an in clause a not-in
0:19
or how do I do maybe a greater than or something like that? Right, substring matching and so on. So here are some of the more common ways
0:26
where you might want to do a query. So the first one is equals. So that's just ==. Not equals is !=, straightforward.
0:36
Like, on the name or on the column in this case it's the name. On the column, we saw they have .des for descending. We'll also have like.in_.
0:47
You may wonder why there's an underscore on the end of in. The reason is in is a keyword. In Python, it's not very selective
0:55
about when it thinks a keyword is a keyword, right? There's no context, so in just basically cannot be used so they use in_.
1:03
You see, like, class_ or _class. Sometimes like when CSS, if you've got to specify CSS class and code, you can't use the word class.
1:12
Anyway, we're going to say, we would like to go find out if the name is in this array or list here, or not in.
1:18
Notice the tilde in the front of the query. It's null; it's equal to None. Now, you can do ands by doing multiple filters
1:25
or you can do an or, a little bit more complicated by calling the or function. This is just some of the stuff you can do. When you check out the docs
1:32
the link is at the bottom below there's a bunch of things you can do. It's a very, very flexible system. Finally, get the full story.
1:41
SQLAlchemy is created by Mike Bayer and a long time ago, can't believe it back in April of 2015, I interviewed him
1:49
about what he's been doing in SQLAlchemy, the origins. So back then, it was created in 2005 that was the 10 year anniversary of it.
1:56
If you want to listen to the creator of SQLAlchemy talk about it, why he created it his thoughts on unit of work, for example
2:03
drop in over at Talk Python To Me and listen to this interview with Mike Bayer.