我是Python新手。我对Tkinter和Sqlite有意见。我需要在数据库中获取一个特定的单词,在“查找单词”框中输入。
#create function to search a record
def wordSearch():
conn = sqlite3.connect('mydata.db')
c = conn.cursor()
c.execute("SELECT * FROM information WHERE my_word = ?")记录= c.fetchall()打印(记录)
#Loop thru results
print_records = ''
for record in records:
print_records += str(record[0]) + " "+ str(record[1]) + " " + str(record[2])+'\n'
conn.commit()
conn.close()
#create text boxes
wordSearch_entry = Entry(root, width = 30)
wordSearch_entry.grid(row = 5, column = 1, pady = 5 )
#create text box labels
wordSearch_entry_label = Label(root, text = "Find word")
wordSearch_entry_label.grid(row = 5, column = 0, pady = 5)
btn_wordSearch = Button(root, text="Search word", command=wordSearch)
btn_wordSearch.grid(row = 6, column = 0, columnspan = 2, pady = 10, ipadx = 130)发布于 2020-05-15 07:56:44
con = sqlite3.connect('./mydata.db')
c = con.cursor()
def searchWord():
c.execute("SELECT * FROM information WHERE my_word = {} ".format(str(wordSearch_entry.get())) #Ok! We have found the words. Lets print it:
for x in c: #'x' gonna append each result 'c' found.
print(x)
searchWord()发布于 2020-05-15 18:51:58
试试这个:
def wordSearch():
conn = sqlite3.connect('mydata.db')
c = conn.cursor()
c = c.execute("SELECT * FROM information WHERE my_word = ?",[wordSearch_entry.get()])
records = c.fetchall()
print(records)
#Loop thru results
print_records = ''
for record in records:
print_records += str(record[0]) + " "+ str(record[1]) + " " + str(record[2])+'\n'
# wordSearch_entry_label['text']=print_records <--- if you want to show your results on wordSearch_entry_label.
#create text boxes
wordSearch_entry = Entry(root, width = 30)
wordSearch_entry.grid(row = 5, column = 1, pady = 5 )
#create text box labels
wordSearch_entry_label = Label(root, text = "Find word")
wordSearch_entry_label.grid(row = 5, column = 0, pady = 5)
btn_wordSearch = Button(root, text="Search word", command=wordSearch)
btn_wordSearch.grid(row = 6, column = 0, columnspan = 2, pady = 10, ipadx = 130)希望它能帮上忙!
https://stackoverflow.com/questions/61803608
复制相似问题