Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 3.x sqlite3 fetchone()返回None或其他值_Python 3.x_Sqlite_Aggregate Functions - Fatal编程技术网

Python 3.x sqlite3 fetchone()返回None或其他值

Python 3.x sqlite3 fetchone()返回None或其他值,python-3.x,sqlite,aggregate-functions,Python 3.x,Sqlite,Aggregate Functions,我正在学习Python,发现了一个难以解释的行为: 使用sqlite3模块,我正在对现有SQLite表执行SQL查询以获取值。如果SQL WHERE子句中的条件为true,我希望得到一个实值;如果条件为false,我将使用COALESCE处理该案例,并希望返回一个默认值(在我的案例中为“-1”)。 但是,根据下面的代码示例,fetchone()将返回一个None对象,直到我在所选列上应用聚合函数(本例中为MAX())为止 result = cur.execute('''SELECT coales

我正在学习Python,发现了一个难以解释的行为: 使用sqlite3模块,我正在对现有SQLite表执行SQL查询以获取值。如果SQL WHERE子句中的条件为true,我希望得到一个实值;如果条件为false,我将使用COALESCE处理该案例,并希望返回一个默认值(在我的案例中为“-1”)。 但是,根据下面的代码示例,fetchone()将返回一个None对象,直到我在所选列上应用聚合函数(本例中为MAX())为止

result = cur.execute('''SELECT coalesce(pin, '-1') inaccuracies
                                    FROM card where number = 'some_incorrect_number' ''').fetchone()
print(result)
# None -- WHY NONE?

result = cur.execute('''SELECT coalesce(max(pin), '-1') 
                                    FROM card where number = 'some_incorrect_number' ''').fetchone()
print(result)
# ('-1',) -- A CORRECT RESULT
你能解释一下为什么MAX()解决了这个问题,我得到了一个需要的结果,而没有MAX()我却没有得到一个结果吗

如果问题中有一些不准确的地方,很抱歉。谢谢大家!

此查询:

SELECT coalesce(pin, '-1') inaccuracies FROM card where number = 'some_incorrect_number'
如果WHERE子句中的条件未被表中的任何行满足,并且这是预期结果,则返回空结果(无行)。


但是,当您使用聚合函数,如
MAX()
MIN()
COUNT()
SUM()
AVG()
GROUP_CONCAT()
时,如果未找到匹配项,查询总是返回函数结果为
null
(或
COUNT()
的情况下为
0
)(对于
WHERE
子句的条件)或即使表中没有行。

谢谢您的澄清!我得到了我错过的东西。