blowing up count()

38 views
Skip to first unread message

Dave S

unread,
Jan 2, 2017, 1:36:59 PM1/2/17
to web2py-users
I have a controller with

 r = db(db.mytable.PostDate is not None)
 streetval
= r.count()

which yields a ticket with

<class 'sqlite3.OperationalError'>(near "WHERE": syntax error)
[...]
Function argument list

(self=<pydal.adapters.sqlite.SQLiteAdapter object>, *a=('SELECT count(*) FROM WHERE 1;',), **b={})

Variables

a ('SELECT count(*) FROM WHERE 1;',)
b {}
self <pydal.adapters.sqlite.SQLiteAdapter object>
ret undefined
command 'SELECT count(*) FROM WHERE 1;'
self.get_cursor <bound method SQLiteAdapter.get_cursor of <pydal.adapters.sqlite.SQLiteAdapter object>>
).execute undefined

What did I do wrong?
/dps

Anthony

unread,
Jan 2, 2017, 2:40:21 PM1/2/17
to web2py-users
On Monday, January 2, 2017 at 1:36:59 PM UTC-5, Dave S wrote:
I have a controller with

 r = db(db.mytable.PostDate is not None)

The above is not a valid DAL query. It is literally equivalent to:

db(True)

Instead, you must use Python logical operators, as specified in the DAL documentation:

db(db.mytable.PostDate != None)

The presence of the Field object in the above expression overrides the usual behavior of the "!=" operator, returning a DAL Query object rather than a Python boolean value. In your original query, there is no overriding of "is not", so you simply get back a boolean True value (as the Field object itself is not None).

Anthony

Dave S

unread,
Jan 3, 2017, 2:59:24 AM1/3/17
to web2py-users
Ah, okay.  I was rushing away from my machine this morning, so I didn't add that a query that checked for equality with req.vars.stuff worked fine.  Your explanation shows why that worked and the above didn't. 

Thanks again !

/dps

Reply all
Reply to author
Forward
0 new messages