简体   繁体   中英

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

I want to build a search button for my project to fetch all the information related to a particular search ie if two similar firstnames say "Purba" exist in the database (sqlite3), then after the search button is clicked, both the users' details should be shown in the listbox in separate rows.

My code is showing the warning message if the searched data is not present in the database but it is showing information about only one user (say having firstname "purba" and lastname "paik") from the database even if another user (say having firstname "purba" and lastname "das") exists in the database. I want my code to show the information about all the users related to a particular search along with the warning message if the searched data is not present in the database.

Following is the code for fetching data from database:

 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") 

Following is the code for executing the information in the listbox:

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")

You should not call both fetchone() and fetchall() as fetchone() will pop one record from the result set. Then fetchall() will fetch the rest which does not include the first record.

Just call fetchall() is enough:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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