繁体   English   中英

如何从数据库中获取与特定搜索相关的所有信息

[英]How to fetch all the information related to a particular search from database

我想为我的项目构建一个搜索按钮来获取与特定搜索相关的所有信息,即如果数据库中存在两个相似的名字“Purba”(sqlite3),那么在单击搜索按钮后,两个用户的详细信息应该在单独的行中显示在列表框中。

如果搜索到的数据不存在于数据库中,我的代码会显示警告消息,但它只显示数据库中一个用户的信息(比如名字为“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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM