请帮忙。我正在创建一个小的GUI字典,并试图将接口链接到数据库。我可以正确地将数据(单词和定义)输入到数据库中,但我似乎无法在数据库中查找单词,以便在我的GUI的输出框中显示相应的定义。
模块1(主程序):
from tkinter import *
import libraryentrySQL
import sqlite3
import os
def click():
entered_text = entry.get() #collect text from text entry box
output.delete(0.0,END) #clears text box - start clearing from 0.0 (from line 0) to END (after last character)
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
try:
definition = c.execute("SELECT definition FROM Dictionary WHERE word=%s", (entered_text))
except:
definition = "No word found in dictionary, try again!"
output.insert(END, definition) #this inserts the contents of variable 'definition' at the beginning (END) - because it was cleared before, END is the at the start
def clickentry(): #this function is run when the 2nd button (entry is pressed)
def definition_submitted(word, definition):
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute("INSERT INTO Dictionary VALUES (?, ?)", (word, definition))
new_db.commit()
new_db.close()
definition_window = libraryentrySQL.DefinitionWindow(window, definition_submitted) #this creates the object 'definition window' and passes to it 'the window variable'
#so that it can have a canvas
#and also passes the function 'definition_submitted' so that as the new word and definition are entered
#in the this object (second window) it can be passed into the function and the dictionary updated
window = Tk()
window.title("My Little Dictionary")
#Create the Label
Label(window, text="Enter the word you want defining:").grid(row=0, column=0, sticky=W)
#create entry box
entry=Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)
#create submit button
Button(window, text="Submit", width=5, command=click).grid(row=2, column=0, sticky=W)
#create second label
Label(window, text="\nDefinition").grid(row=3, column=0, sticky=W)
#create text box
output=Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, sticky=W)
#create submit button to open enter new definition window
Button(window, text="Enter a New Definition", width=20, command=clickentry).grid(row=5, column=0, sticky=W)
#Create the Dictionary.db if not already present
if not os.path.isfile("dictionary.db"):
new_db = sqlite3.connect('dictionary.db')
c=new_db.cursor()
c.execute('''CREATE TABLE Dictionary
(word text,
definition text)''')
c.execute('''INSERT INTO Dictionary VALUES
('Algorithm', 'Step by step instructions to complete a task')''')
new_db.commit()
new_db.close()
window.mainloop()
模块2(输入单词和定义窗口):
from tkinter import *
class DefinitionWindow(Toplevel):
def __init__(self, root, click_callback):
Toplevel.__init__(self, root)
self.click_callback = click_callback
self.title("Library entry")
#Create the Label
Label(self, text="Enter the word you want to add:").grid(row=0, column=0, sticky=W)
#create entry box
self.word_entry=Entry(self, width=20, bg="light green")
self.word_entry.grid(row=1, column=0, sticky=W)
#create second label
Label(self, text="\nDefinition").grid(row=2, column=0, sticky=W)
#create entry box
self.definition_entry = Entry(self, width=50, bg="light green")
self.definition_entry.grid(row=3, column=0, sticky=W)
#create submit button
Button(self, text="Submit", width=5, command=self.clicked).grid(row=4, column=0, sticky=W)
def clicked(self):
self.click_callback(self.word_entry.get(), self.definition_entry.get()) #when this function is called (on submit button click) it takes the entered
#word and definition and assigns it to click_callback, which is an attribute of DefinitionWindow??
self.destroy() #after the word and definition are added to the call_back variable, the frame containing this instance of definition window is closed
我做错了什么?我知道这是“SELECT”SQL命令不正确。我们将非常感激地提供任何帮助。感谢
答案 0 :(得分:1)
这不是Python中SQL查询的工作方式。
execute
方法不返回值而是返回游标。
如果没有找到任何内容,则不会引发异常,并且光标只是空的。
在except
块中盲目处理所有异常将隐藏任何编程错误。
此外,参数标记不是%s
而是?
。
最后,具有单个值的Python元组必须包含逗号以将其与单个表达式区分开来:
c = new_db.cursor()
c.execute("SELECT definition FROM Dictionary WHERE word = ?", (entered_text,))
for row in c:
definition = row[0]
break
else:
definition = "No word found in dictionary, try again!"
output.insert(END, definition)