Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在函数默认参数中使用SQlite NULL?_Python_Python 3.x_Sqlite_Select_Where Clause - Fatal编程技术网

Python 如何在函数默认参数中使用SQlite NULL?

Python 如何在函数默认参数中使用SQlite NULL?,python,python-3.x,sqlite,select,where-clause,Python,Python 3.x,Sqlite,Select,Where Clause,我想从ip为空的sqlite数据库中获取域。我还想使功能,以便我可以得到特定的IP域在未来。我编写了如下函数: 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 --

我想从ip为空的sqlite数据库中获取域。我还想使功能,以便我可以得到特定的IP域在未来。我编写了如下函数:

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)
但它不返回任何行。当我使用其他值进行测试时,它可以工作,但无法读取ip=null的位置。
如何返回默认ip为空的行?

不能使用相等运算符检查空值;为此,需要特殊表达式
为NULL
。一些数据库提供了一个空安全的相等运算符(通常,MySQL具有
,Postgres支持标准
不同)。SQLite为此提供了
IS

SELECT domain FROM domains WHERE ip IS ?

它起作用了。这是否意味着我们应该做一个或两个:1。在sqlite中避免NULL,并使用适当的值;2.避免使用“WHERE ip=”并始终使用“WHERE ip IS”,无论发生什么情况。?