0

Id like to get domains from sqlite database where ip is NULL. I also want to make function so I can get domains with specific IPs in the future. I wrote function like this:

def get_domains(self, ip=None):
    """gets all the domains from domain table

    Keyword Arguments:
        ip {str} -- IP of the domain (default: {None})

    Returns:
        sqlite3.Row -- rows of the selected domains
    """
    sql = "SELECT domain FROM domains WHERE ip = ?"
    try:
        self.cursor.execute(sql, (ip,))
        rows = self.cursor.fetchall()
        return rows
    except Error as e:
        print(e)

but it returns no rows. It works when I test with other values, but can't make it to read WHERE ip = null. How do I return rows where default ip is null?

1 Answer 1

1

You can't check for nullness with the equality operator; for this you need special expression IS NULL. Some databases offer a null-safe operator for equality (typically, MySQL has <=>, Postgres supports the standard IS [NOT] DISTINCT FROM). SQLite offers IS for that.

SELECT domain FROM domains WHERE ip IS ?
Sign up to request clarification or add additional context in comments.

1 Comment

It works. Does this means that we should do one or both of this: 1. avoid NULL in sqlite and use proper value; 2. avoid "WHERE ip =" and always use "WHERE ip IS" no matter what.?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.