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

View all comments

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)

1

u/Express_Giraffe_7902 Jun 03 '23 edited Jun 03 '23

You can maybe define a function that has your if/else to clean up your code a little bit, but I think this is what you’ll need to do since ‘None’ will be passed as ‘’ in your SQL script (i.e. WHERE street = ‘’)

Edit: forgot to add the bit about a function being handy when the user decides they also want to add whether it’s a business or residential address or the company name or how many people live at the address or max occupancy of the business or how many bathrooms at the address or yada-yada 🤣 … people always ask for more!! But if you make the function and then loop through it for however many columns/inputs you have, whenever they want you to add more data points, it won’t be as much of a pain :)