[Tutor] problem with building dict w/ SQlite SELECTS in loop

Alan Gauld alan.gauld at btinternet.com
Wed Sep 17 01:30:20 CEST 2008


"Che M" <pine508 at hotmail.com> wrote

> The reason is merely lack of experience and because of how I was
> using the fetchall() in one list comprehension to build each list...

> > (heck, you could select code, start, style form codes -- pull all 
> > the
> > information you need in a single query, and skip the loop

> I think I need the loop because the style will be multiple styles 
> and
> I need to take the codes that go with each style, so I am querying
> style by style, so to speak.

Check out the GROUP BY and ORDER BY clauses of a SELECT
statement. I'm pretty sure you could select all the stuff you want
using a combination of those two clauses. It should be much faster
and lead to more maintainable code too. Something like:

select code from codes where code != '' and style = ? and
> start >= ? and start < ?

SELECT code, start
FROM codes
WHERE code != "" AND start >= begin AND start < end
GROUP BY style
ORDER BY style

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list