WojonsTech
WojonsTech

Reputation: 1277

SQLite adding columns in row in query

I am trying to do a little bit of math in in a sqlite call. I have two columns that I want to add their value then check to see if they are less than a value that I input.

    c = self.db.cursor()
    c.execute("BEGIN EXCLUSIVE TRANSACTION");
    c.execute("SELECT ID as id,task FROM tube WHERE state=0 OR (state=1 & ts+ttr<? ) ORDER BY ID ASC LIMIT 1", (time.time(),))
    task = c.fetchone()
    print task
    if task != None:
        ts = time.time();
        c.execute("UPDATE tube SET state=1,ts=? WHERE ID=?", (ts, task['id']))
        task['ts'] = ts
    else:
        task = None

    self.db.commit()
    return task

From what I can tell its not doing this operation. It still returns a row but not based on the logic I am providing.

Upvotes: 0

Views: 73

Answers (1)

Janne Karila
Janne Karila

Reputation: 25197

You are using the bitwise and operator & where you probably want a logical AND.

The bitwise operators have higher precedence that the comparisons (=, <), while the logical ones have lower. Therefore state=1 & ts+ttr<? is very different from state=1 AND ts+ttr<?

Upvotes: 4

Related Questions