Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输入输出_Python_Sqlite_Tkinter - Fatal编程技术网

Python 输入输出

Python 输入输出,python,sqlite,tkinter,Python,Sqlite,Tkinter,我正在使用db文件创建一个数据查找器,并使用tkinter条目。但是在控制台中,它返回输出[],代码如下 import sqlite3 import tkinter db = sqlite3.connect ('covidjakartadb.db') window = tkinter.Tk() window.geometry("500x300") label = tkinter.Label(window, text="Please enter a area") label.pack()

我正在使用db文件创建一个数据查找器,并使用tkinter条目。但是在控制台中,它返回输出
[]
,代码如下

import sqlite3
import tkinter

db = sqlite3.connect ('covidjakartadb.db')

window = tkinter.Tk()
window.geometry("500x300")

label = tkinter.Label(window, text="Please enter a area")
label.pack()

entry = tkinter.Entry(window)
entry.pack()

select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)

cursor = sqlite3.Cursor(db)

cursor.execute(select_all)

def Search_Completed():
    label = tkinter.Label(window,  text="aaaaa")

Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()



positive = cursor.fetchall()

print (positive)

window.mainloop()

该项目今天到期,因此今天的答案将非常有用

从数据库中选择的代码需要在
搜索功能中完成。正如所写的,它甚至在GUI打开之前运行

搜索应该使用条目的内容,
Entry.get()
而不是
Entry

import sqlite3
import tkinter

db = sqlite3.connect ('covidjakartadb.db')

window = tkinter.Tk()
window.geometry("500x300")

label = tkinter.Label(window, text="Please enter a area")
label.pack()

entry = tkinter.Entry(window)
entry.pack()

def Search_Completed():
    # select_all = "SELECT * FROM locations WHERE '%{0}%'".format(entry)
    # select_all = "SELECT * FROM locations WHERE '%{0}%'".format( entry.get() )
    # This will work
    select_all = "SELECT * FROM locations WHERE City LIKE '%{0}%'".format( entry.get() )        
    # Or this.
    # select_all = "SELECT * FROM locations WHERE City == '{0}'".format( entry.get() )
    cursor = sqlite3.Cursor(db)
    cursor.execute(select_all)
    positive = cursor.fetchall()
    print (positive)
    label = tkinter.Label(window,  text="aaaaa")

Button = tkinter.Button(window, text="Search data", command=Search_Completed)
Button.pack()
window.mainloop()

我无法测试代码,因为我没有数据库,因此可能会有一些错误。

由于我没有您的数据库,很难复制,但从代码中我可以说,数据库正在从您的查询返回“[]”。您是否尝试手动查询数据库?SQL语句中的WHERE子句无效。它应该类似于
“从字段名为“{0}%”的位置选择*。.format(entry.get())
。用您想要使用的实际字段替换
field\u name
。@figbeam是的,我已经尝试过了,但仍然不起作用。我将在
光标=
之前打印
选择\u all
,以确保查询是您期望的查询。谢谢!但是我想通过在条目中键入城市名称来搜索它的数据,它提取我将提供的所有数据db文件当前生成的sql语句是“SELECT*FROM locations WHERE“%Jakarta%”,它需要是“…类似于“%Jakarta%”的城市”或“…city==”Jakarta“。LIKE将“Jak”与雅加达匹配只有拼写正确时才匹配。