public inbox for [email protected]  
help / color / mirror / Atom feed
From: Daniele Varrazzo <[email protected]>
To: listas <[email protected]>
Cc: [email protected]
Subject: Re: Adaptation in psycopg3
Date: Wed, 25 Nov 2020 19:02:42 +0000
Message-ID: <CA+mi_8Z=nPj4ZiS7Z8CyPtOgAosMdUd_wh6o0ZeRs2rsgNBMBg@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <CA+mi_8ZYBu5V1hcKvsnMqgKayGvyMRJAMdMQVXvZ02QXsnt9yA@mail.gmail.com>
	<[email protected]>
	<CA+mi_8YBYfTjfhBvbz8ivRD4wz6d1MExjhWTcCHAXDiX3tKzeA@mail.gmail.com>
	<[email protected]>

On Wed, 25 Nov 2020 at 18:00, listas <[email protected]> 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





view thread (7+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Adaptation in psycopg3
  In-Reply-To: <CA+mi_8Z=nPj4ZiS7Z8CyPtOgAosMdUd_wh6o0ZeRs2rsgNBMBg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox