How to execute an UPDATE query without string concatenation/interpolation?

From: "W(dot) Matthew Wilson" <matt(at)tplus1(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: How to execute an UPDATE query without string concatenation/interpolation?
Date: 2011-01-08 17:33:11
Message-ID: AANLkTimhP0qkaTqz=LHgmBkpzzqG1kAd_aAJNC1OFiAA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

I want to write a function that I can use like this

>>> update_foo(foo_id=1, colA=11)

and it do this internally:

cursor.execute("""
update foo
set colA = (%s)
where foo_id (%s)""", [11, 1])

And I want to pass in more than just a single column to update, like this, too:

>>> update_foo(foo_id=1, colA=11, colB=12, colC=13)

and it should do this:

cursor.execute("""
update foo
set
colA = (%s),
colB = (%s),
colC = (%s)

where foo_id (%s)""", [11, 12, 13, 1])

I'm having a really hard time doing this without building up strings
and then appending them together. Is there some better way?

Thanks in advance.

--
W. Matthew Wilson
matt(at)tplus1(dot)com
http://tplus1.com

Responses

Browse psycopg by date

  From Date Subject
Next Message Suporte 2011-01-08 21:15:02
Previous Message Daniele Varrazzo 2011-01-06 16:50:03 Test message