【问题标题】:Displaying SQLite specific data in a Tkinter GUI在 Tkinter GUI 中显示 SQLite 特定数据
【发布时间】:2020-05-14 17:27:00
【问题描述】:

我是 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)

【问题讨论】:

  • 您没有为查询提供参数。

标签: python sqlite tkinter


【解决方案1】:
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()

【讨论】:

  • 它给了我一个错误 sqlite3.OperationalError: no such column: name There is no conumn "name", but there are the row "name"
  • 尝试将{} 更改为'{}'。但最好使用占位符。
【解决方案2】:

试试这个:

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)

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多