Thank you All for the detailed examples.<div><br></div><div>I tried them all in IDLE and i finally understood them.</div><div><br></div><div>Thanks for your patience with me until i understand!<br><br><div class="gmail_quote">
2012/1/10 Nick Dokos <span dir="ltr"><<a href="mailto:nicholas.dokos@hp.com">nicholas.dokos@hp.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Νικόλαος Κούρας <<a href="mailto:nikos.kouras@gmail.com">nikos.kouras@gmail.com</a>> wrote:<br>
<br>
> On 10 Ιαν, 03:11, Ian Kelly <<a href="mailto:ian.g.ke...@gmail.com">ian.g.ke...@gmail.com</a>> wrote:<br>
> > 2012/1/9 Íéêüëáïò Êïýñáò <<a href="mailto:nikos.kou...@gmail.com">nikos.kou...@gmail.com</a>>:<br>
> ><br>
> > > if the MySQL query was:<br>
> ><br>
> > > cursor.execute( '''SELECT host, hits, agent, date FROM visitors WHERE pin =<br>
> > > %s ORDER BY date DESC''', pin )<br>
> ><br>
> > > can you help me imagine how the mysql database cursor that holds the query<br>
> > > results would look like? I must somehow visualize it in order to understand<br>
> > > it!<br>
> ><br>
> > You can think of it as a pointer, traversing over one row of the<br>
> > result set at a time.  Hopefully this will come out legibly:<br>
> ><br>
> > -----------------------------------------------<br>
> > | HOST | HITS | AGENT | DATE |<br>
> > -----------------------------------------------            -------------<br>
> > | foo     | 7       | IE6       | 1/1/11 |   <----   | cursor |<br>
> > -----------------------------------------------            -------------<br>
> > | bar     | 42     | Firefox  | 2/2/10 |<br>
> > -----------------------------------------------<br>
> > | baz    | 4       | Chrome | 3/3/09 |<br>
> > ------------------------------------------------<br>
><br>
> Database cursor is the pointer that iterates over the result set one<br>
> row at a time?<br>
> I though that it was the name we give to the whole mysql result set<br>
> returned my cursor.execute.<br>
><br>
> ><br>
> ><br>
> > > Also what happend if the query was:<br>
> > > cursor.execute( '''SELECT host FROM visitors") ?<br>
> ><br>
> > > the result would have to be something likelike?<br>
> ><br>
> > > -----------------<br>
> > > |somehost1|<br>
> > > -----------------<br>
> > > |somehost2|<br>
> > > -----------------<br>
> > > |somehost3|<br>
> > > -----------------<br>
> > > .....................<br>
> > > .....................<br>
> > > |somehost n|<br>
> > > -----------------<br>
> ><br>
> > > So what values host, hits, agent, date would have in 'for host, hits, agent,<br>
> > > date in<br>
> > >  dataset' ? Every row has one string how can that be split in 4?<br>
> ><br>
> > Why don't you try it and see what happens?  But to spare you the<br>
> > suspense, you would get:<br>
> ><br>
> > ValueError: need more than 1 value to unpack<br>
> ><br>
> > Because you can't unpack a 1-length tuple into four variables.  The<br>
> > code assumes that the query is selecting exactly 4 columns.<br>
><br>
><br>
> -----------------------------------------------<br>
> | HOST    | HITS    | AGENT     | DATE |<br>
> -----------------------------------------------<br>
> | foo     | 7       | IE6       | 1/1/11 |<br>
> -----------------------------------------------<br>
> | bar     | 42      | Firefox   | 2/2/10 |<br>
> -----------------------------------------------<br>
> | baz     | 4       | Chrome    | 3/3/09 |<br>
> -----------------------------------------------<br>
><br>
> In this line:<br>
> for host, hits, agent, date in dataset:<br>
><br>
> 'dataset' is one of the rows of the mysql result or the whole mysql<br>
> result set like the table above?<br>
><br>
> I still have trouble understanding this line :(<br>
<br>
You can think of it as a list of tuples. Forget about cursors and<br>
databases for now. If l is a list [1, 2, 3, 4] you iterate over it like<br>
this:<br>
<br>
for x in l:<br>
  print x<br>
<br>
and you get each element of the list[fn:1]. Similarly if l is a list of tuples<br>
l = [(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)] you can iterate over the<br>
list:<br>
<br>
for x in l:<br>
  print x<br>
<br>
In this case, x is going to be (1,2,3,4) the first time through the loop, (5,6,7,8)<br>
the second time and so on. You can break each x apart within the loop:<br>
<br>
for x in l:<br>
  t1, t2, t3, t4 = x<br>
  print x, t1, t2, t3, t4<br>
<br>
or you can break it apart like this - it's essentially the same thing:<br>
<br>
for t1, t2, t3, t4 in l:<br>
  print t1, t2, t3, t4<br>
<br>
You have been encouraged repeatedly to try these things interactively:<br>
please do so with the above examples and all will become clear.<br>
<br>
Going back to cursors and databases: you *can* think of 'dataset' as<br>
being a list of tuples - a list of all the query results, but with one<br>
proviso. The difference when you use a cursor is that `dataset' may<br>
be a lazy list (an "iterator"): instead of the whole set of results<br>
being in memory at the same time, the system will take care of doing<br>
whatever is necessary to get more of the results when it needs them. But<br>
the behavior is the *same* in the sense that the output that you get is<br>
the same (you will only see differences if you are keeping an eye on how<br>
much memory and how much time your program is using).<br>
<br>
Nick<br>
<br>
Footnotes:<br>
<br>
[fn:1] ... and, no, you *can't express* this as<br>
"the first time it is<br>
<br>
for x in 1:<br>
  ...<br>
<br>
and the second time it is<br>
<br>
for x in 2:<br>
  ...<br>
<br>
" as you asked in another email. That's arrant nonsense: x takes<br>
successive values in the list l for every iteration of the for<br>
loop. This is elementary Python (nay, elementary programming, period).<br>
</blockquote></div><br></div>