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

Поиск
Список
Период
Сортировка
От W. Matthew Wilson
Тема How to execute an UPDATE query without string concatenation/interpolation?
Дата
Msg-id AANLkTimhP0qkaTqz=LHgmBkpzzqG1kAd_aAJNC1OFiAA@mail.gmail.com
обсуждение исходный текст
Ответы Re: How to execute an UPDATE query without string concatenation/interpolation?  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
Список 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@tplus1.com
http://tplus1.com

В списке psycopg по дате отправления:

Предыдущее
От: Daniele Varrazzo
Дата:
Сообщение: Test message
Следующее
От: Suporte
Дата:
Сообщение: