Ask coding questions

← Back to all posts
I'm trying to save a `datetime` object in my repl.it's database and it returns an error:
h
adamtuby (0)

this is what I wrote:

config = datetime.datetime.now()
db['timestampBefore'] = config

it returns the following error:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from distributing import start
  File "/home/runner/Cryptomon-Sales-Bot/distributing.py", line 10, in <module>
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from distributing import start
  File "/home/runner/Cryptomon-Sales-Bot/distributing.py", line 11, in <module>
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from distributing import start
  File "/home/runner/Cryptomon-Sales-Bot/distributing.py", line 11, in <module>
    db['timestampBefore'] = config
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 486, in __setitem__
    self.set(key, value)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 495, in set
    self.set_raw(key, _dumps(value))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/replit/database/database.py", line 56, in dumps
    return json.dumps(val, separators=(",", ":"), cls=DBJSONEncoder)
  File "/usr/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
ValueError: Circular reference detected

Does anyone know how to save a datetime object in my database?
thanks in advance!

Comments
hotnewtop
19ecal (230)

Use this code

db["timestampBefore"]=datetime.datetime.now().isoformat()

Then you can convert back into a datetime object with this code

print(datetime.datetime.fromisoformat(db["timestampBefore"]))
adamtuby (0)

@19ecal dude you just solved the problem that crashed my whole bot. I can't thank you enough!