Steven D'Aprano wrote:
On Fri, 18 Jun 2010 03:54:05 pm Lang Hurst wrote:
Is there a way to just return the values when using sqlite3?
If I:
names = c.execute('select names from students')
for name in names: print names
What is c? A Cursor or a Connection object? It probably doesn't make any
real difference, but it could, and so you should say.
I'm doing my best. c was a cursor. Perhaps not the best naming convention.
[snip]
I doubt it. Did you mean this?
(u'Cleese, John',)
Note the comma at the end. A subtle difference, but a HUGE one.
Punctuation is important: it's what makes the difference between:
Yup, you're right.
In Python, the comma makes it a tuple, rather than a meaningless pair of
parentheses.
is there a way to just return
Cleese, John
Jones, Terry
Gillaim, Terry
No, because you're not *returning* anything, you're *printing*, and
printing a tuple prints the opening and closing brackets plus the
repr() of each internal object.
I guess I should have been more clear. By return, I suppose I meant
assign. I'm trying to pull the names from rows of data and append each
name to a ListStore, for entry completion.
I was using "print" just to show what I was hoping to get into the
ListStore, and at that point, I was only getting the tuple, not the object.
This is fundamental stuff: objects are not their printable
representation, any more than you are a photo of you.
What is printed is not the same as the object itself. Every object has
its own printable representation. In some cases it looks exactly the
same as the way you enter the object as a literal:
[snip]
In this case, the object returned by execute is a list of tuples. If
there is only a single object in each tuple, it is still a tuple, and
it still prints as a tuple. If you want to print something other than a
tuple, don't print the tuple:
names = c.execute('select names from students')
for t in names:
name = t[0] # Extract the first item from each tuple.
print name
Of course you can combine the two lines inside the loop:
print t[0]
OK.
It seems a shame to have to run the answers through a stip process.
It would be more of a shame if execute sometimes returned a list of
tuples, and sometimes a list of other objects, with no easy way before
hand of knowing what it would return.
Actually, I know you were being facetious, but with my skills so poor,
often that seems like what is happening. I'm killing myself right now
trying to troubleshoot glade, python and entrycompletion. I know it
will soon be as clear as a tuple vs an object; I just hope that
transition happens soon. Thanks again for walking me through the
terminology, it is appreciated.
--
There are no stupid questions, just stupid people.
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor