ajanakos
ajanakos

Reputation: 119

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.

Upvotes: 0

Views: 44

Answers (1)

Justin Poehnelt
Justin Poehnelt

Reputation: 3454

Update: Simply switch your between parameters.

This:

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

Should be:

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

Upvotes: 1

Related Questions