Replit DB is not loading data into variables

116 Views Asked by At

i'm using replits database but when i try to load it in it returns an error

any ideas?

cookie = db[name]
cookiepc = db[name + "cookiepc"]
increase = db[name + "increase"]

the error is

Traceback (most recent call last):
  File "main.py", line 25, in <module>
    cookiepc = db[name + "cookiepc"]
  File "/home/runner/Cookie-clicker/venv/lib/python3.8/site-packages/replit/database/database.py", line 439, in __getitem__
    raw_val = self.get_raw(key)
  File "/home/runner/Cookie-clicker/venv/lib/python3.8/site-packages/replit/database/database.py", line 479, in get_raw
    raise KeyError(key)
KeyError: 'shdfgwbdhfbadwcookiepc'
1

There are 1 best solutions below

0
On

According to the documentation for replit-py, a KeyError is raised when an attempt is made to read from a key that doesn't exist in the database.

You can use db.get to specify a default value for if the key doesn't exist:

print(db.get("b", "default")) # default
db["b"] = "pie"
print(db.get("b", "default")) # pie