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

Che M pine508 at hotmail.com
Tue Sep 16 22:43:42 CEST 2008



> I'm quite willing to help on this, but it's just not quite
> clear enough what's happening. Can I suggest that you
> post a *really small, self-contained* example which demonstrates
> the problem you're having. You might actually realise what's
> wrong as you do that, or if not someone like me can just run
> the example and see what you mean and point things out.

> Your code is missing just too many things for me
> to work out which bit is doing what. (And it's a bit
> awkward because of the long lines).
> 
> Try posting something like this:
> 
> <code>
> import sqlite3
> 
> db = sqlite3.connect (":memory:")
> db.execute ("CREATE TABLE codes (code)")
> db.executemany ("INSERT INTO codes VALUES (?)", [(i,) for i in range (10)])
> 
> q = db.cursor ()
> for n_try in range (3):
>   q.execute ("SELECT * FROM codes WHERE code > ?", [n_try])
>   q.fetchall ()
> 
> q.close ()
> 
> </code>
> > 
> and say what you think should happen, and what does
> happen. Note that the entire code I've posted there
> can be dropped straight into an interpreter and run
> which makes it much easier for people who are willing
> to help to see what you're trying to do.
> 
> TJG
> 

I normally do try to include a small runnable sample, but in this
case it seemed too extricated into the rest of my code.  As it happens,
as I was fooling around with variations of your code sample, it reinforced
my feeling that nothing was wrong with my loop and SQL query as such,
and it was then I noticed that it was the *list* that was wrong.

When I built my list, I built it using a .split() from a textbox field of 
comma-separated words.  But, I had written:

self.style_list = swlf.style_value.split(',')

when I should have written it as:

self.style_list = swlf.style_value.split(', ')


and that missing whitespace caused my list entries to each have
one whitespace in front of all but the very first item in the list, and
so of course they didn't match the words in the database.  So that
was the problem--I had been fixating on the SQL queries and it was
an improper use of .split() earlier in the code.  I have a feeling this
isn't the first time someone has made this mistake.

Thank you for your help,
Che


_________________________________________________________________
See how Windows connects the people, information, and fun that are part of your life.
http://clk.atdmt.com/MRT/go/msnnkwxp1020093175mrt/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080916/207e0d90/attachment.htm>


More information about the Tutor mailing list