Z
Z
Zorgios2021-08-13 13:36:04
Python
Zorgios, 2021-08-13 13:36:04

How to update all entries by list id?

There is a database with columns ID(int), Status(int)
How, having a list of IDs (of N pieces), make a request to update data for all records from this list of IDs?
(You need to change the value of the Status field)
How will the request look like? Do not send N requests for each id separately...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-08-13
@o5a

If performance is not critical (the id list is not very large), then you can simply use executemany (it actually runs separate updates, but automatically according to the list).

cur.executemany('update mytab set status = 1 where id = ?', ((id, ) for id in idlist) )

For better performance, it would be more correct to use 'update ... where id in (...)', forming a query by formatting with passing parameters, but there are also some peculiarities there.
PS didn't notice the SQLalchemy tag. If you need it for her, then the answer will not work :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question