简体   繁体   中英

Python + SQLite3 parameters failing

The parameters on the last cur.execute statement aren't working.

The below code doesn't error out but cur.fetchone() return rows of value (0,) when they shouldn't

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()

I get the right results with:

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

...while only inserting the user = ? variable.

Update: Simply switch your between parameters.

This:

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

Should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM