Handling (None,) Query Results

From: Hagen Finley <hagen(at)datasundae(dot)com>
To: psycopg(at)lists(dot)postgresql(dot)org, psycopg(at)postgresql(dot)org
Subject: Handling (None,) Query Results
Date: 2020-12-05 15:57:06
Message-ID: 5f75fbb2-6da2-60f3-5488-9013d1328361@datasundae.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Hello,

I was thinking ‘finally, something I know how to do’ but alas simple
sum(revenue) where select statements in psycopg2 have proven to be more
complex than I imagined.

First, there’s the Decimal tuple parsing which I can do (albeit somewhat
unnaturally)  (Decimal('450992.10'),)

cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = 'Big Company' AND stage
LIKE 'Commit%';")
commitd1 = cur.fetchone()
conn.commit()
commitd2 = commitd1[0]

if type(commitd2)is not None:commit =int(commitd2)

else:
commit =0

450992.10

<class 'int'>

If there is a better way to get to int I'd be all ears.

Second, there’s the NoneType  (None,) result from queries with no values.

cur.execute("SELECT SUM(revusd) FROM sfdc where saccount = 'Big Company' AND stage
LIKE 'Win%';")
wind1 = cur.fetchone()
conn.commit()
wind2 = wind1[0]

if type(wind2)is int:win =int(wind2)

else:
win =0

My goal is to return 0.00 when there are no results and an int when
there are results using the same code. Right now my if statements are
different:

if type(commitd2)is not None:

if type(wind2)is int:

Possibly ignoring my fledgling attempts to solve this problem, is there
a simple method by which people convert the:

1. (Decimal('450992.10'),) to a <class 'int'> 450992.10?

2. (None,) to 0.00?

 Thanks for your thoughts on this question.

Best,

Hagen Finley

Fort Collins, CO

Responses

Browse psycopg by date

  From Date Subject
Next Message Christophe Pettus 2020-12-05 16:02:25 Re: Handling (None,) Query Results
Previous Message Karsten Hilbert 2020-12-02 16:23:01 Re: Executing on the connection?