2016-09-19 8 views
0

我正在處理我的第一個(較大的)python應用程序,但我遇到了一些問題。我試圖使用web.py導入從表中選擇條目(我使用這個,因爲我稍後將使用Web前端)。web.py選擇返回沒有結果,但該列表的長度(也是SQL Promt中的手動查詢返回結果)

下面是我的(簡化的)代碼:

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') <-- 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]) 

下面是從蟒輸出:

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

下面是MySQL的輸出:

mysql> 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) 

結論:表包含條目,並且select/query語句能夠部分獲取它們(它獲取長度,但不是實際值?)

我嘗試過多種方式,但目前我無法獲得我想要的東西。我想從我的表中選擇數據並在我的代碼中使用它。

謝謝了幫助

回答