Python 如何在函数默认参数中使用SQlite NULL?
我想从ip为空的sqlite数据库中获取域。我还想使功能,以便我可以得到特定的IP域在未来。我编写了如下函数: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 --
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”,无论发生什么情况。?