[Tutor] SQLite3 DB Field Alphabetizing

Alan Gauld alan.gauld at btinternet.com
Wed Oct 13 10:01:44 CEST 2010

"David Hutto" <smokefloat at gmail.com> wrote

> Below is the output before converting:
> [(u'.hjvkjgfkj/bdgfkjbg', u''), (u'bbbbbbbbbbbbuuzzzzzzzzz', u'Pie
> Chart'), (u'jgkgyckghc', u''), (u'kfhhv ', u''), (u'khfhf', u''),
> (u'test', u''), (u'test10', u''), (u'test2', u'Dashed/Dotted'),
> (u'test3', u'Pie Chart'), (u'test346w43', u''), (u'test4', u'Scatter
> Plot'), (u'test5', u''), (u'test6', u''), (u'test7', u''),
> (u'test7uyuy', u''), (u'test8', u''), (u'test9', u''), 
> (u'testgraph',
> u''), (u'u;s;juf;sfkh', u''), (u'zzrerhshhjrs', u'')]

And there is the answer to your question.
The string that you thought started with ; actually starts with a 'u'
But you strip the 'u;' off that entry so it appears to be badly 

So the next question is why do you strip the 'u' off?

I suspect the answer to that also lies here.
I suspect you think you need to strip all the 'u's from the front of
these results but in fact those 'u's lie outside the string, they
are a part of Python's representation of a unicode string,
not a part of the string.

So if you remove the call to strip() your code will return
the result you expected.

> > What happens if you execute the SQL at the sqlite interactive
> > prompt? Does it give the same result?
> haven't used the sqlite prompt yet, Haven't needed to yet.

You should. Consider it like the >>> prompt in Python, an invaluable
aid in experimenting and developing working SQL statements.
Not to use the interactive prompts while writing your code is
to lose a great deal of the productivity power of Python and
SQLite. Why debug a whole script when you can debug  a
single line with instant feedback?


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list