Explanation about for
Ian Kelly
ian.g.kelly at gmail.com
Mon Jan 9 20:11:32 EST 2012
2012/1/9 Νικόλαος Κούρας <nikos.kouras 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 |
------------------------------------------------
> 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.
More information about the Python-list
mailing list