How can I find null values with SELECT query in psycopg?

2024/12/9 21:58:14

I am using psycopg2 library in python and the INSERT query works good when I insert null Value with None, but when I want to do SELECT null values, with None doesn't return any.

cur.execute("SELECT id FROM registro WHERE id_movil = (%s);", (None,))

This query doesnt return any rows, i dont know where is the error.

Anyone know how to make SELECT query's to find null values in a DB?

Answer

First thing to do is find out what query it's turning your command into:

print(cur.mogrify("SELECT id FROM registro WHERE id_movil = (%s);", (None,)))

If that gives you anything other than an IS NULL codition check, it won't get NULLs from the table.

Specifically, if you see the phrase = NULL, you'll know it's not being translated correctly(1) and you'll have to do something like:

if var is None:cur.execute("SELECT id FROM registro WHERE id_movil IS NULL;")
else:cur.execute("SELECT id FROM registro WHERE id_movil = (%s);", (var,))

Or, if you know you're always looking for NULL values (as your use of the constant None seems to indicate), just use:

cur.execute("SELECT id FROM registro WHERE id_movil IS NULL;")

(1) It's quite possible that only the %s is replaced with a NULL when the argument is None, and it's not quite clever enough to go back and change = into IS (or <> into IS NOT).

This would explain why this works:

cur.execute("INSERT into mytbl value (%s);", (None,))

(because substitution of only the %s gives the command you want), but anything with = NULL will not act as expected, unless you've been burnt by it enough times to know what to expect :-).

https://en.xdnf.cn/q/73162.html

Related Q&A

Pause and continue stopwatch

I am trying to create stopwatch. I have done it but I would like to pause and continue the time whenever I want. I have tried some things but I have no idea how to do it. Is there anybody who would exp…

How do I escape `@` letter from SQL password in connection URI [duplicate]

This question already has an answer here:handle @ in mongodb connection string(1 answer)Closed 9 years ago.when you connect to mongodb using python from SQLAlchamey, we use mongodb://username:password@…

Set WTForms submit button to icon

I want a submit button that displays an icon rather than text. The button is a field in a WTForms form. I am using Bootstrap and Open Iconic for styling and icons. How do I set the submit field to d…

what is the significance of `__repr__` function over normal function [duplicate]

This question already has answers here:Purpose of __repr__ method?(6 answers)Closed 5 years ago.I am trying to learn python with my own and i stucked at __repr__ function. Though i have read lots of p…

Using celery with Flask app context gives Popped wrong app context. AssertionError

Im more or less using the setup to run Celery tasks using your flask app context from here: http://flask.pocoo.org/docs/0.10/patterns/celery/Im getting the same error message as Create, manage and kill…

How do I reduce the verbosity of chromedriver logs when running it under selenium?

My jenkins failure reports for my functional tests are full of lines like this:selenium.webdriver.remote.remote_connection: DEBUG: Finished Request selenium.webdriver.remote.remote_connection: DEBUG: P…

How to model python properties in UML diagram

What is a good practice to model Python properties in a UML class diagram? Properties themselves are class objects, their getter and setter are class functions. From Outside the class they look like i…

Linear regression with tensorflow

I trying to understand linear regression... here is script that I tried to understand: A linear regression learning algorithm example using TensorFlow library. Author: Aymeric Damien Project: https://g…

Are null bytes allowed in unicode strings in PostgreSQL via Python?

Are null bytes allowed in unicode strings?I dont ask about utf8, I mean the high level object representation of a unicode string.BackgroundWe store unicode strings containing null bytes via Python in …

Why the irrelevant code made a difference?

I am thinking to make a progress bar with python in terminal. First, I have to get the width(columns) of terminal window. In python 2.7, there is no standard library can do this on Windows. I know mayb…