In python I have a list of tuples
Each tuple has four fields, the first field is the row identifier that would be used in a where clause in a SQL update statement.
tuple: (id, status, department, manager_email)
update_tuple_list = [
(1, 'ACTIVE', 'UC563', '[email protected]'),
(2, 'ACTIVE', 'UC921', '[email protected]'),
(3, 'DISABLED', 'UC983', '[email protected]'),
...
]
Using Python 3, psycopg ver 3.1.8
How would I do a bulk update?
For table user, the statement would be:
update user set
status = %s,
department = %s,
manager_email = %s
where id %s; # where the value of 'id' is the first field in the tuple.
I can rearrange the tuple to put the row id id
at the end of the tuple if that would help.
How do I feed the example list of tuples to a statement like the above and do a single bulk update?
Is there a better more understandable way to do this using Python 3 | psycopg Ver 3.1.8 (NOT psycopg2)
I have seen stack overflow answers involving psycopg2 and I simply do not understand the response or the solution provided there, and also I am not using psycopg2. It’s just not clear.
It’s very simple with a bulk insert because there is no where
clause’. With a bulk update, there is a where clause to identify the row that is being updated.
I have searched on this and tried to find a simple solution using the latest python version and library version, and nothing comes up.
Any and all help to solve this would be greatly appreciated.
2
Answers
Maybe it depends what exactly you require from a "bulk update". If you’d just like everything to happen in one transaction, I think you might be good with something like this (untested!).