-
Title:
Order By Solution - Web Development
-
Description:
-
Okay, so here's my answer.
-
The SQL is Select * From Links,
-
where submitter ID equals 62443,
-
order by submitted time
-
ASC, for ascending.
-
And these are the constraints of the question--62443 and submission time.
-
And then what I'm doing is I'm iterating over the results,
-
making a link,
-
appending the link's ID to the list of results,
-
and then I'm returning results.
-
Okay, so let's go ahead and give this a run to make sure it works.
-
We see our answer is 15, 18, 21.
-
Those are the correct link IDs.
-
My answer looks the way it does
-
because I've been following the form we've been kind of working in--
-
the general structure of this function.
-
But if I were actually to make a function that does just this--
-
just returns the IDs of the links--
-
there's a couple things that are wasteful in here
-
that I'll go ahead and clean up and show you.
-
So the first thing is we don't need to select * from links.
-
All we care about is the IDs.
-
So we'll just select the IDs.
-
Here we're creating a link object
-
for every row we get back.
-
Well, we don't actually need the link object.
-
If we're just going to get the IDs from the results,
-
we can just return those nearly directly.
-
So let's get rid of this loop
-
and instead change it to something like this.
-
Okay, so I simplified this quite a bit.
-
This syntax here is a list comprehension.
-
It's a simple way of making a list.
-
So we still run our SQL.
-
This time we're selecting ID.
-
And since SQLite Library in Python returns tuples of the results,
-
and in this case we're only asking for the ID,
-
we know the first column is going to be the ID.
-
So for t in c--so for the tuple in the cursor--
-
make a list of the first element of each of those tuples,
-
store that in results, and return it.
-
Let's go ahead and print this.
-
And the answer is the same--15, 18, 101.
-
Okay, so just a little extra Python there for you.
-
Let's move along.