Re: Adaptation in psycopg3

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: listas <listas(at)soft-com(dot)es>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: Adaptation in psycopg3
Date: 2020-11-25 19:02:42
Message-ID: CA+mi_8Z=nPj4ZiS7Z8CyPtOgAosMdUd_wh6o0ZeRs2rsgNBMBg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Wed, 25 Nov 2020 at 18:00, listas <listas(at)soft-com(dot)es> wrote:

> Thank for your replies, I will use "=any(params list)" in psycopg3

No problem. But if it was not clear, this is something that works
already in psycopg2 too: it could be useful if you want to port code
later.

> The second question is: if psycopg3 is going to do the automatic cast of
> the types, will it be able to distinguish between a json and a list of
> values?.
> Example:
>
> data = ["a", "b", "c"]
> idList = [4,7,2]
>
> cursor.execute("update mytable set jsfield=%s where id = any(%s)",
> (data, idList))
>
> What will be the correct syntax in this case?

You would do like in psycopg2. There isn't a single json type in
python (it could be a list, dict, number, None...) so there is a
"Json" wrapper to tell psycopg to pass e.g. a json number rather than
a number-number:
https://www.psycopg.org/docs/extras.html?highlight=json#json-adaptation

What you would do, both in psycopg2 and 3, would be something like:

cursor.execute("update mytable set jsfield=%s where id = any(%s)",
(Json(data), idList))

Code like this should work in both versions.

Cheers

-- Daniele

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message listas 2020-11-25 20:05:50 Re: Adaptation in psycopg3
Previous Message listas 2020-11-25 18:00:21 Re: Adaptation in psycopg3