SQLite准备与未准备的查询 - 不同的结果(PySide,python)

时间:2014-03-12 19:30:32

标签: python sqlite pyside

Python,PySide,SQLite:

我可能错过了一些明显的东西,但我得到了相同(或者我认为)查询的不同结果。我应该看到未准备好的查询的结果(afaik)。

感谢您的帮助!

制备

    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 result http://oi62.tinypic.com/1dy0l1.jpg

1 个答案:

答案 0 :(得分:3)

问题可能在于:

    query.bindValue(":group", filteringCategory)

尝试绑定不是值的参数。我不认为允许绑定列或表名,因为这会改变SQL语句的实际结构。

相反,您需要执行以下操作:

    "SELECT ... :from_date AND :to_date GROUP BY %s" % filteringCategory