Explanation about for

Νικόλαος Κούρας nikos.kouras at gmail.com
Tue Jan 10 04:02:59 EST 2012


On 10 Ιαν, 03:11, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> 2012/1/9 Íéêüëáïò Êïýñáò <nikos.kou... at gmail.com>:
>
> > if the MySQL query was:
>
> > cursor.execute( '''SELECT host, hits, agent, date FROM visitors WHERE pin =
> > %s ORDER BY date DESC''', pin )
>
> > can you help me imagine how the mysql database cursor that holds the query
> > results would look like? I must somehow visualize it in order to understand
> > it!
>
> You can think of it as a pointer, traversing over one row of the
> result set at a time.  Hopefully this will come out legibly:
>
> -----------------------------------------------
> | HOST | HITS | AGENT | DATE |
> -----------------------------------------------            -------------
> | foo     | 7       | IE6       | 1/1/11 |   <----   | cursor |
> -----------------------------------------------            -------------
> | bar     | 42     | Firefox  | 2/2/10 |
> -----------------------------------------------
> | baz    | 4       | Chrome | 3/3/09 |
> ------------------------------------------------

Database cursor is the pointer that iterates over the result set one
row at a time?
I though that it was the name we give to the whole mysql result set
returned my cursor.execute.

>
>
> > Also what happend if the query was:
> > cursor.execute( '''SELECT host FROM visitors") ?
>
> > the result would have to be something likelike?
>
> > -----------------
> > |somehost1|
> > -----------------
> > |somehost2|
> > -----------------
> > |somehost3|
> > -----------------
> > .....................
> > .....................
> > |somehost n|
> > -----------------
>
> > So what values host, hits, agent, date would have in 'for host, hits, agent,
> > date in
> >  dataset' ? Every row has one string how can that be split in 4?
>
> Why don't you try it and see what happens?  But to spare you the
> suspense, you would get:
>
> ValueError: need more than 1 value to unpack
>
> Because you can't unpack a 1-length tuple into four variables.  The
> code assumes that the query is selecting exactly 4 columns.


-----------------------------------------------
| HOST    | HITS    | AGENT     | DATE |
-----------------------------------------------
| foo     | 7       | IE6       | 1/1/11 |
-----------------------------------------------
| bar     | 42      | Firefox   | 2/2/10 |
-----------------------------------------------
| baz     | 4       | Chrome    | 3/3/09 |
-----------------------------------------------

In this line:
for host, hits, agent, date in dataset:

'dataset' is one of the rows of the mysql result or the whole mysql
result set like the table above?

I still have trouble understanding this line :(



More information about the Python-list mailing list