web.py select returning no results, but length of that list does (also manual query in sql promt returns results)
<p>I am working on my first (bigger) python application but I am running into some issues. I am trying to select entries from a table using the web.py import (I am using this since I will be using a web front-end later).</p>

<p>Below is my (simplified) code:</p>

<pre><code>db = web.database(dbn='mysql', host='xxx', port=3306, user='monitor', pw='xxx', db='monitor')
dict = dict(hostname=nodeName)
#nodes = db.select('Nodes', dict,where="hostName = $hostname")
nodes = db.query('SELECT * FROM Nodes') &lt;-- I have tried both, but have comparable results (this returns more entries)
length = len(list(nodes))
print(length)
print(list(nodes))
print(list(nodes)[0])
</code></pre>

<p>Below is the output from python:</p>

<pre><code>0.03 (1): SELECT * FROM Nodes
6 &lt;-- Length is correct
[] &lt;-- Why is this empty?
Traceback (most recent call last):
  File "monitor.py", line 30, in &lt;module&gt;
    print(list(nodes)[0]) &lt;-- If it is empty I can't select first element
IndexError: list index out of range
</code></pre>

<p>Below is mySQL output:</p>

<pre><code>mysql&gt; select * from monitor.Nodes;
+--------+-------------+
| nodeId | hostName    |
+--------+-------------+
|      1 | TestServer  |
|      2 | raspberryPi |
|      3 | TestServer  |
|      4 | TestServer  |
|      5 | TestServer  |
|      6 | TestServer  |
+--------+-------------+
6 rows in set (0.00 sec)
</code></pre>

<p>Conclusion: table contains entries, and the select/query statement is able to get them partially (it gets the length, but not the actual values?)</p>

<p>I have tried mutliple ways but currently I am not able to get the what I want. I want to select the data from my table and use it in my code.</p>

<p>Thanks for helping</p>

<p>Thanks to the people over at reddit I was able to solve the issue: <a href="https://www.reddit.com/r/learnpython/comments/53hdq1/webpy_select_returning_no_results_but_length_of/" rel="nofollow">https://www.reddit.com/r/learnpython/comments/53hdq1/webpy_select_returning_no_results_but_length_of/</a></p>

<p>Bottom line is: the implementation of the query method already fetches the data, so the second time I am calling list(nodes) the data is already gone, hence the empty result.</p>

<p>Solution is to store the list(nodes) in a variable and work from that.</p>

python mysql database select web.py