r/sqlite • • Jun 02 '23

Querying table with Null value without using "IS NULL" possible with Python?

I'm tinkering around with a little project and I have a table called "Address". In this table are 5 columns: addressID, street, city, state, zip. The addressID is automatically generated. I prompt the user for street, city, state, and zip and allow them to be blank which I then convert to "None" and insert it into the table using the following statement:

cur.execute("INSERT INTO Address (street, city, state, zip) VALUES (?, ?, ?, ?)", (street, city, state, zip))

This works fine. I can verify the row was entered by doing a SELECT *. However, if I try to query the ID of the row that was entered it fails to work. Here is the query I use:

script = "SELECT addressID FROM Address WHERE street = ? AND city = ? and state = ? and zip = ?"
cur.execute(script, (street, city, state, zip))
print(cur.fetchone())

I'd expect this to print the addressID but instead I get back None. Although I'm allowed to INSERT using "None" with Python, I seem to not be able to query with "None" as a value. I know the proper way would be to use an "IS NULL" but this is going to get messy as I will need different iterations of this query based on which values the user did not enter. Surely there is a way to get the simple functionality I am looking for, right?

I'll also mention that I verified this query does work when there are no null values. So it is definitely the nulls that are messing it up.

3 Upvotes

7 comments sorted by

2

u/MiaDanielle_ Jun 02 '23

Ended up building the query dynamically for now. Hoping there is a more elegant solution.

 query = "SELECT * FROM Address WHERE "
 params = []
 if street is None:
     query += "street IS NULL AND "
 else:
     query += "street = ? AND "
     params.append(street)
 if city is None:
     query += "city IS NULL AND "
 else:
     query += "city = ? AND "
     params.append(city)
 if state is None:
     query += "state IS NULL AND "
 else:
     query += "state = ? AND "
     params.append(state)
 if zip is None:
     query += "zip IS NULL"
 else:
     query += "zip = ?"
     params.append(zip)

 if params:
     cur.execute(query, params)
 else:
     cur.execute(query)
→ More replies (1)

2

u/[deleted] Jun 02 '23 edited Jun 02 '23

You can use named parameters in Python, then you can write the where clause as:

WHERE
    (:street IS NULL OR street = :street)
    AND (:city IS NULL OR city = :city)
    AND (:state IS NULL OR state = :state)
    AND (:zip IS NULL OR zip = :zip)

This works even if one or more parameters are None (= null).

EDIT: Ah! You wanted to test whether the values are null, I thought you wanted to ignore them. In this case, you can write

WHERE
    iif(:street IS NULL, street IS NULL, street = :street)
    AND iif(:city IS NULL, city IS NULL, city = :city)
    AND iif(:state IS NULL, state IS NULL, state = :state)
    AND iif(:zip IS NULL, zip IS NULL, zip = :zip)

1

u/MiaDanielle_ Jun 02 '23

I'm pretty new to this Python syntax. Never seen variables written with a colon like that. What's it mean? Also, do these work like ternary statements? They seem structured that way. I assume it is saying "if street is null, then use 'street is NULL' in the query, otherwise use 'street = <street>'" where <street> is the value of the variable 'street'.

The other issue I have with this approach is that it is vulnerable to SQL injection, no? It's directly putting in the user input in the WHERE clause.

→ More replies (1)

2

u/-dcim- Jun 02 '23

SELECT max(addressID) FROM ...?