Skip to content

Instantly share code, notes, and snippets.

@evan-burke
Last active June 3, 2020 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evan-burke/8e8a3317e889716bca3e742305c2da30 to your computer and use it in GitHub Desktop.
Save evan-burke/8e8a3317e889716bca3e742305c2da30 to your computer and use it in GitHub Desktop.
update query using psycopg2 execute_values
# Updates are a little tricky using psycopg2.extras.execute_values(), and documentation is a little sparse.
# http://initd.org/psycopg/docs/extras.html
db_connection_string = "dbname=EDITME host=EDITME"
# uuids, as strings
my_data = ['6ef0f42a-63da-4edb-9a11-5e146cb337ac','e7b1e961-0a68-4c4f-a716-e0959593f27d','1f82c9a5-00c3-4bd8-8c50-0ede441b4e91']
query = """update my_table t
set bool_field = true
from (VALUES %s) AS d (my_data)
where t.uuid_field = d.data::uuid"""
with psycopg2.connect(db_connection_string) as conn:
with conn.cursor() as cur:
psycopg2.extras.execute_values(cur, insert_query, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment