William Ross
William Ross

Reputation: 3910

psycopg2 execute is changing null values to None

I have a database connection, and derive a cursor from it. Then I execute a SQL statement which is a constant I have defined.

curs = conn.cursor()
curs.execute(SQL)

When I loop through cursor to view the records, records have null values replaced with 'None'. I can confirm via PgAdmin that the values in Postgres are null and not 'None'.

How do I get the cursor execution to keep the values as null rather than replacing with None?

Upvotes: 0

Views: 1763

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51529

you can coalesce nulls to string 'null' in query itself, eg:

t=# with n(v) as (values ('foo'),(null))
select v,coalesce(v,'null') from n;
  v  | coalesce
-----+----------
 foo | foo
     | null
(2 rows)

Upvotes: 1

Related Questions