Python、PySide、SQLite:
我可能错过了一些明显的东西,但我用相同的(或者我认为的)查询得到了不同的结果。我应该看到未准备的查询的结果(据我所知)。
感谢您提供的所有帮助!
准备:
filteringCategory = "store"
from_date = '2014-03-01'
to_date = '2014-03-29'
query.prepare("SELECT s.name AS store, SUM(price) AS total_price FROM expenses AS e JOIN expenses_to_store AS ets ON e.expenses_id = ets.expenses_id JOIN store AS s ON ets.store_id = s.store_id WHERE date BETWEEN :from_date AND :to_date GROUP BY :group")
query.bindValue(":from_date", from_date)
query.bindValue(":to_date", to_date)
query.bindValue(":group", filteringCategory)
query.exec_()
未准备:
query.exec_("SELECT price, s.name AS store, SUM(price) AS total_price FROM expenses AS e JOIN expenses_to_store AS ets ON e.expenses_id = ets.expenses_id JOIN store AS s ON ets.store_id = s.store_id WHERE date BETWEEN '2014-03-01' AND '2014-03-29' GROUP BY store")
最佳答案
问题可能出在以下行:
query.bindValue(":group", filteringCategory)
它正在尝试绑定(bind)一个不是值的参数。我认为不允许绑定(bind)列名或表名,因为这会改变 SQL 语句的实际结构。
相反,您需要执行以下操作:
"SELECT ... :from_date AND :to_date GROUP BY %s" % filteringCategory
关于python - SQLite 准备好的查询与未准备的查询 - 不同的结果(PySide,python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361937/