我在 Python 中有以下函数:
def get_emo_results(emo, operator):
cursor.execute("SELECT avg(?) FROM [LIWC Post Stats] "
"WHERE goals_scored {0} goals_taken "
"GROUP BY post_number "
"ORDER BY post_number ASC "
"LIMIT ?".format(operator), [emo, posts_limit])
print "SELECT avg({1}) FROM [LIWC Post Stats] "\
"WHERE goals_scored {0} goals_taken "\
"GROUP BY post_number "\
"ORDER BY post_number ASC "\
"LIMIT {2}".format(operator, emo, posts_limit)
return [x[0] for x in cursor.fetchall()]
我调用它get_emo_results('posemo', '>')
并将这个输出到标准输出:
SELECT avg(posemo) FROM [LIWC Post Stats] WHERE goals_scored > goals_taken GROUP BY post_number ORDER BY post_number ASC LIMIT 200
但是,函数本身返回
[0.0, 0.0, 0.0, 0.0, 0.0, ... 0.0]
我将 stdout 中的确切表达式复制并粘贴到我打开的 SQLite 进程中,我得到了这个:
1.8730701754386
2.48962719298246
2.18607456140351
2.15342105263158
2.33107456140351
2.11631578947368
2.37100877192982
1.95228070175439
2.01013157894737
...
3.37183673469388
所以根本不是0。尽管使用相同的查询,为什么我的 Python 函数返回不同的东西?我究竟做错了什么?
编辑:
现在,当我去掉问号并直接格式化字符串时,它就可以工作了。为什么在这种情况下参数化查询不起作用?