0

Im trying to insert a datetime into pgsql with

f'UPDATE userdata SET claimed = {fclaim} WHERE userid = {user.id}'

heres how the fclaim variable is made

   nextclaim = datetime.datetime.now() + datetime.timedelta(hours=24)
   
   fclaim = nextclaim + datetime.timedelta(hours=24)

When i try to insert fclaim into postgresql i get the following error

  File "/home/server/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/server/Documents/blutonium/extensions/points.py", line 162, in daily
    isClaimed = blutapi.getclaimed(ctx.author)
  File "/home/server/Documents/blutonium/blutapi.py", line 445, in getclaimed
    db.run(sql)
  File "/home/server/.local/lib/python3.6/site-packages/pg8000/core.py", line 954, in run
    self._run_cursor.execute(sql, params, stream=stream)
  File "/home/server/.local/lib/python3.6/site-packages/pg8000/core.py", line 351, in execute
    self._c.execute_unnamed(self, operation, args)
  File "/home/server/.local/lib/python3.6/site-packages/pg8000/core.py", line 1262, in execute_unnamed
    args = make_args(vals)
  File "/home/server/.local/lib/python3.6/site-packages/pg8000/core.py", line 213, in make_args
    return tuple(vals[p] for p in placeholders)
  File "/home/server/.local/lib/python3.6/site-packages/pg8000/core.py", line 213, in <genexpr>
    return tuple(vals[p] for p in placeholders)
KeyError: '37' 

using python 3.6.11 on ubuntu 20.04

1
  • Please show a complete example of how you pass the values in. Also it it generally not a good idea to format the string yourself(SQL injection issues). You should use the parameter passing shown format or numeric. Or one of the others listed here dbapi under paramstyle Commented Aug 4, 2020 at 23:44

1 Answer 1

1

Following the docs, here's an example of doing an UPDATE with pg8000:

conn.run(
    "UPDATE userdata SET claimed = :fclaim WHERE userid = :user_id", fclaim=fclaim,
    user_id=user.id)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.