0

So I created a Gui with a field where the user inserts the id of a document and other information and then inserts it in the database. He can also search for the document using the correct id.(Search for documents that exist works just fine) What I want to do is display an error message when the users enters an id and no corresponding document is found. This is just a piece of the code.

mongoHost= "localhost"
myclient = pymongo.MongoClient("mongodb://localhost:27018/")
db = myclient["realEstateDB"]
mycol = db["houses"]


idquestion= Label(frame,text="Property ID:",bg="#D3D3D3",fg="black", font='Helvetica 13 bold')
idquestion.grid(row=2,column=1)
idfield= Entry(frame)
idfield.grid(row=2,column=2,padx=10, pady=10)
id=idfield.get()

for mydict in mycol.find({"Property ID": id}):
        if mydict[{"Property ID": id}] not in mycol:
        message= Label(frame2, text='No such entry', bg="#D3D3D3", fg="black",font='Helvetica 13 bold')
        message.pack()
arissalon
  • 35
  • 4
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) – stovfl Sep 17 '19 at 18:33
  • This bit of code is not something we can test. That said I suspect what you are needing is a tracking variable IE: `is_data_found = False`. Then if something is found set value to true and if not leave value false. Then after your loop run an if statement that checks if found and if not found then update label or add label or some popup message. – Mike - SMT Sep 17 '19 at 18:38
  • If you are using ID to search, you should use [`find_one`](https://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one) instead. It will return the document directly or `None` which you can work on without the need of the for loop. – Henry Yik Sep 18 '19 at 02:07

0 Answers0