![](/img/trans.png)
[英]How To Fetch Related Category Products in Python Django From Database Table?
[英]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.