Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exists always false when load existing db #37

Closed
chrisdlangton opened this issue Sep 27, 2018 · 1 comment
Closed

exists always false when load existing db #37

chrisdlangton opened this issue Sep 27, 2018 · 1 comment

Comments

@chrisdlangton
Copy link

test script;

#main.py

import os
import pickledb

DBCHANGES = False
base_dir = '/tmp'
filename = 'example.db'

db = pickledb.load(os.path.join(base_dir, filename), False)

key = 123456789
value = 'blah'

if not db.exists(key):
  db.set(key, value)
  DBCHANGES = True

if db.exists(key):
  print db.get(key)

if DBCHANGES:
  db.dump()
  print 'dumped db'

results in;

~$ python main.py
blah
dumped db
~$ python main.py
blah
dumped db
~$ cat /tmp/example.db
{"123456789": "blah", "123456789": "blah"}%

And we now have duplication in the databse because due to the failure of the exists method

@patx
Copy link
Owner

patx commented Oct 1, 2018

This is happening because the key you are testing to see if it exists is stored in json as a str, not as an int like your script (line 10).

>>> db.exists(123456789)
False
>>> db.exists("123456789")
True

As you mentioned this only happens when loading an existing json file, if you were to set a key as an int you would be able to call it as an int until you dumped the database to a json file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants