Python + SQLite3参数失败

时间:2015-08-03 04:18:57

标签: python parameters sqlite

最后一个cur.execute语句的参数无效。

以下代码没有错误输出,但cur.fetchone()返回值为(0,)的行不应该

with conn:
    flag = 0
    today = (datetime.date.today() - datetime.timedelta(20)).strftime("%Y-%m-%d")
    prev_prev_day = (datetime.date.today() - datetime.timedelta(22)).strftime("%Y-%m-%d")
    cur = conn.cursor()
    cur.execute("SELECT EXISTS(SELECT user, COUNT(user) cnt FROM logins GROUP BY user HAVING cnt > 1)")
    if cur.fetchone()[0] == 1:
        flag + 1
        cur.execute("SELECT user, COUNT(user) cnt FROM logins GROUP BY user HAVING cnt > 1")
        for item in cur.fetchall():
            variables = (item[0], today, prev_prev_day,)
            # problem in with this statement
            cur.execute("SELECT COUNT(*) FROM logins WHERE user = ? AND seen_date BETWEEN ? AND ?", variables)
            print cur.fetchone()

我得到了正确的结果:

            cur.execute("SELECT COUNT(*) FROM logins WHERE user = ? AND seen_date BETWEEN '2015-07-11' AND '2015-07-13'", variables)

...只插入用户=?变量

1 个答案:

答案 0 :(得分:1)

更新:只需切换参数即可。

此:

variables = (item[0], today, prev_prev_day,)

应该是:

variables = (item[0], prev_prev_day, today)