2013-03-21 49 views
0

它不允許我從我的數據庫訪問我的提示,出現錯誤。無法從我的數據庫中爲我的tkinter GUI檢索數據

代碼:

def do_question(self): 
self.func1() 
#myGlobal + 1 
if myGlobal >5: 
    import MathsvadersReal 

SQL = 'SELECT * FROM tblQuestion' 
cursor = Databaseconnector.SELECT(SQL) 
rows = cursor.fetchall() 
random_row = random.choice(rows) 

print random_row.QuestionID, random_row.Question, random_row.Hint, random_row.A1, random_row.A2, random_row.A3, random_row.A4, random_row.CorrectAnswer 

# create welcome label 
self.label1 = Tkinter.Label(self, bg ='yellow', text = (random_row.Question)) 
self.label1.grid(row = 0, column = 6, columnspan = 2, sticky = 'E') 

self.label111 = Tkinter.Label(self, bg ='red', text = (random_row.QuestionID)) 
self.label111.grid(row = 0, column = 1, columnspan = 4, sticky = 'W') 

提示代碼:

def homepage_link(self): 
    SQL = 'SELECT Hint FROM tblQuestion WHERE QuestionID = %s' % self.label111 
    cursor = Databaseconnector.SELECT(SQL) 
    rows = cursor.fetchall() 
    tkMessageBox.showinfo("Hint", rows[0]['Hint']) 

錯誤:

IndexError:列表索引超出範圍

+0

的IndexError表明'使用fetchall()'沒有返回任何行。 – voithos 2013-03-21 16:09:35

回答

1

你逝去的小部件的字符串表示形式,而不的text選項。的homepage_link第一行應是:

SQL = 'SELECT Hint FROM tblQuestion WHERE QuestionID = %s' % self.label111['text'] 
+0

當我改變,我得到一個不同的錯誤:TypeError:行索引必須是整數,而不是str – 2013-03-21 17:17:20

+0

@SmithJr使用'行[0] [0]'而不是'行[0] ['提示']'。 – 2013-03-21 17:39:00

+0

TypeError:字符串索引必須是整數 – 2013-03-21 20:36:30