Python 如何从数据库中获取与特定搜索相关的所有信息
我想为我的项目构建一个搜索按钮,以获取与特定搜索相关的所有信息,即,如果数据库(sqlite3)中存在两个类似的名字,称为“Purba”,则在单击搜索按钮后,两个用户的详细信息应分别显示在列表框中的行中 如果数据库中不存在搜索到的数据,则我的代码显示警告消息,但它仅显示来自数据库的一个用户的信息(例如,名为“purba”和姓为“paik”),即使数据库中存在另一个用户(例如,名为“purba”和姓为“das”)。 我希望我的代码显示与特定搜索相关的所有用户的信息,如果数据库中没有搜索到的数据,则显示警告消息 以下是从数据库获取数据的代码:Python 如何从数据库中获取与特定搜索相关的所有信息,python,sqlite,tkinter,python-3.7,Python,Sqlite,Tkinter,Python 3.7,我想为我的项目构建一个搜索按钮,以获取与特定搜索相关的所有信息,即,如果数据库(sqlite3)中存在两个类似的名字,称为“Purba”,则在单击搜索按钮后,两个用户的详细信息应分别显示在列表框中的行中 如果数据库中不存在搜索到的数据,则我的代码显示警告消息,但它仅显示来自数据库的一个用户的信息(例如,名为“purba”和姓为“paik”),即使数据库中存在另一个用户(例如,名为“purba”和姓为“das”)。 我希望我的代码显示与特定搜索相关的所有用户的信息,如果数据库中没有搜索到的数据,则
def search(self,EmpID="",Firstname="",Lastname="",DOB="",Age="",Gender="",Address="",Marital_Status="",Email="",Mobile=""):
print("Database:search method called",EmpID)
con=sqlite3.connect("Employee.db")
cur=con.cursor()
cur.execute("select * from employee where EmpID=? or Firstname=? or Lastname=? or DOB=? or Age=?
or Gender=? \
or Address=? or Marital_Status=? or Email=? or Mobile=?",(EmpID,Firstname,Lastname,DOB,Age,Gender,Address,Marital_Status,Email,Mobile))
if(cur.fetchone()) is not None:
row=cur.fetchall()
con.close()
print(EmpID,"Database:search method finished\n")
return row
else:
tkinter.messagebox.showwarning("EMPLOYEE MANAGEMENT SYSTEM","doesn't exist")
以下是执行列表框中信息的代码:
def SearchDatabase():
print("employee:search method called")
for row in p.search(EmpId.get(),firstname.get(),lastname.get(),dob.get(),age.get(),\
gender.get(),address.get(),marsta.get(),email.get(),mobile.get()):
Emplist.insert(END,row,str(""))
print("employee:search method finished\n")
您不应该同时调用
fetchone()
和fetchall()
,因为fetchone()
将从结果集中弹出一条记录。然后fetchall()
将获取不包括第一条记录的其余记录
只要调用fetchall()
就足够了:
def search(self,EmpID="",Firstname="",Lastname="",DOB="",Age="",Gender="",Address="",Marital_Status="",Email="",Mobile=""):
print("Database:search method called",EmpID)
con=sqlite3.connect("Employee.db")
cur=con.cursor()
cur.execute("""select * from employee \
where EmpID=? or Firstname=? or Lastname=? or DOB=? or Age=? \
or Gender=? or Address=? or Marital_Status=? or Email=? or Mobile=?""",
(EmpID,Firstname,Lastname,DOB,Age,Gender,Address,Marital_Status,Email,Mobile))
row = cur.fetchall()
if len(row) > 0:
print('found')
else:
messagebox.showwarning("EMPLOYEE MANAGEMENT SYSTEM","doesn't exist")
con.close()
print(EmpID,"Database:search method finished")
return row