Is NULL, is not working in my if statement

Hello I am having a problem where the is NULL, is not working in my if statement. I am attempting to remove “None” from the code’s output, but neither is NULL or == “None” are working

Code

import mysql.connector

mydb = mysql.connector.connect(
  host="100.0.0.1",
  user="root",
  password="",
  database="data_logic"
)

mycursor = mydb.cursor()

subject = input("Enter your value: ")
vals = (subject,)

sql = "SELECT * FROM classez LEFT JOIN software ON FK_SOFT_ID = SOFT_ID LEFT JOIN text_books ON FK_BOOK_ID = BOOK_ID LEFT JOIN tools ON FK_TOOL_ID = TOOL_ID WHERE SUBJECT_NUM = %s"

mycursor.execute(sql,vals)
myresult = mycursor.fetchall()

print("\nThese are the results:\n")
for row in myresult:
    if row is None:
        print("")
    else:
        print(row)

Sample Output

1, 'Campbell Biology', 12, 'Pearson', 'Urry', 'Lisa', '9780135188743', 'E-book', 94.99, 'http://amazon.com/Campbell-Biology-2-download', 0, None, None, None, None, None, None, None)

Are you trying to remove the None values from the row? In that case, you will need to go through each element of the row. The entire row itself is not None, it’s a list with stuff in it. So if row is None isn’t helping you there.

for row in myresult:
    row_without_None = [value for value in row if value is not None]
    print(row_without_None)

Should output

[1, 'Campbell Biology', 12, 'Pearson', 'Urry', 'Lisa', '9780135188743', 'E-book', 94.99, 'http://amazon.com/Campbell-Biology-2-download', 0]
1 Like

Thank you very much that worked :slight_smile: