I can’t delete key in dbm

Asked

Viewed 54 times

2

I made a basic program to store some data and I am in trouble if the user chooses the option to delete some stored dictionary key. The function that erases data in my program is this:

def EraseData(name):
'''
Apaga uma chave do dicionário do banco de dados.
'''
db = dbm.open('data.db', 'c')
del db[name]
db.close()

When I search the entries in my database I see that NOTHING has been deleted. It just doesn’t work.

This is the link to all my code from GIT:https://github.com/JeffersonCarvalh0/Armazenamento-de-dados/blob/master/armazenamento.py

  • I think you can help here. http://stackoverflow.com/questions/5844672/delete--element-from-a-dictionary

  • But I don’t want to keep the database and delete a copy, I really want to change it. However, even using the del I can’t. I realized that by interactive mode, when I run exactly the same lines I have in the function, I can delete the key, but by function does not work.

  • So, you need to further detail your error. The function is correct, it works perfectly. Your program, however, has some errors not working properly. I fixed the errors and everything worked perfectly. Are you sure that’s the problem you exposed?

2 answers

1

Tested here, and using direct in interactive mode works the del (although the documentation of this module is very weak and does not mention the del and neither did he show up at the dir of object dbm).

My hypothesis is this: you have this function EraseData and in it you open, modify and close the bank - however, I believe that if the same bank is open while you do these strokes, it may be that when it closes the other copy of the bank that is open the keys that exist there are valid. You have the database opened in a variable in the context that calls the EraseData (or in any other thread/context?)

I do not believe that this module will work well opening more than one connection at the same time to the same file - even if it has been thought to work like this: my suggestion is to open this database at the beginning of the program, and only close it at the exit. (use a clause try...finally to ensure that the close be called - and, or pass the database instance to all functions that access it, or create a class in which one of the attributes is the database instance - and promote all functions to methods of that class, or simply leave a global avariable with the database instance (not as bad in Python as in C or other languages, because "global" variables are not really "global" - they are module variables valid only in the namespace of that module)

1


Like I said, you need to go into more detail about your mistake. The function you created works perfectly, but nevertheless there are some mistakes in your program that prevent it from working.

First, syntax errors, in the Checkrg function, name if correctly.

Second, in the Showlist function, is giving that db is not eternal. So, itere on the bank keys:

for key in db.keys():
    ...

Finally, the part that actually prevents the Erasedata function from working is the lifting of the exception FileAlreadyExists in the Searchdata function. The exception is being raised before the connection to the database is closed. This prevents the Erasedata function from opening a new connection (generating a new exception) and definitely delete an entry.

Soon:

def SearchData(nome):
    db = dbm.open('data.db', 'c')
    if name in db:
        db.close()
        raise FileAlreadyExists

After this has been done, the function works perfectly. Otherwise, we need more details of the execution of your program.

  • Ah, now yes... as for the syntax errors, I don’t know where they came from, because yesterday when I created this topic, the code was error-free (it must have been due to some confusion I made while trying to pass this code to GIT, because I’m still learning how to work it). As for my function ShowList, here it works perfectly. Finally, I hadn’t really seen this problem in function SearchData. I made the suggested fix and now the program works smoothly. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.