Re: Query with boolean parameter

From: Paolo De Stefani <paolo(at)paolodestefani(dot)it>
To: Christophe Pettus <xof(at)thebuild(dot)com>
Cc: Psycopg <psycopg(at)postgresql(dot)org>
Subject: Re: Query with boolean parameter
Date: 2022-03-19 19:30:32
Message-ID: 6955bcd2bba7feaddb480df53fc8b558@paolodestefani.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Il 19/03/2022 17:35 Christophe Pettus ha scritto:
>> On Mar 19, 2022, at 05:10, Paolo De Stefani <paolo(at)paolodestefani(dot)it>
>> wrote:
>> Switching to psycopg 3 i have to consider many more differences than i
>> expected
>
> There have been some changes in the way psycopg2 does parameter
> substitution, although that one is an interesting case! You might
> consider using IS DISTINCT FROM as Daniele suggested, or just =,
> depending on how you want nulls handled.

thanks to all for the suggestions i thimk i will use the '=' operator
what do you mean with 'depending on how you want nulls handled' ???

test4=# create table test (a text, b boolean);
CREATE TABLE
test4=# insert into test values ('aaa', true), ('bbb', false), ('ccc',
null);
INSERT 0 3
test4=# select * from test;
a | b
-----+---
aaa | t
bbb | f
ccc |
(3 rows)

test4=# select * from test where b = true;
a | b
-----+---
aaa | t
(1 row)

test4=# select * from test where b is true;
a | b
-----+---
aaa | t
(1 row)

test4=# select * from test where b = null;
a | b
---+---
(0 rows)

test4=# select * from test where b is null;
a | b
-----+---
ccc |
(1 row)

I will use '= True' or '= False' in psycopg cur.execute with parameter
substitution and 'IS NULL' without parameter when i need to check the
null value

--
Paolo De Stefani

In response to

Browse psycopg by date

  From Date Subject
Next Message Rich Shepard 2022-04-04 22:02:40 cur.execute() syntax error
Previous Message Adrian Klaver 2022-03-19 17:34:32 Re: Query with boolean parameter