mardi 4 mai 2021

Get values by using fetchone and verify when the value is null

I'm new to Python and was working on a script which executes a postgres query and pulls its result, it's just a number:

con = psycopg2.connect("dbname=mydb user=postgres host=192.168.0.10")
cur = con.cursor()
myvar='TEST'
cur.execute("SELECT get_id('myvar')")
    my_id = cur.fetchone()
    print(my_id)

The results I get are like these depending on the myvar value:

(144,) (140,) (141,)

Sometimes when there's no value returned by the query, I get this: (None,) I was expecting something like "null" (similar to what I get when I run this on the DB) but that wasn't the case.

Question number one is: why do I get the values surrounded by a parenthesis and the comma at the end?

Question two is: How "if" may work when the value is 'None'?

I've tried this: if my_id=='None': if my_id=='(None,)':

but didn't work..

Aucun commentaire:

Enregistrer un commentaire