mems=[135123123,135123154,150012323]
rows=odb(odb.player_master.link_id.belongs(mems)).select()
SELECT player_master.link_id, player_master.last_name, player_master.first_name FROM logismos.player_master WHERE (player_master.link_id IN (135123123,135123154))
>>> named_params = {'dept_id':50, 'sal':1000}
>>> query1 = cursor.execute('SELECT * FROM employees WHERE department_id=:dept_id AND salary>:sal', named_params)
>>> query2 = cursor.execute('SELECT * FROM employees WHERE department_id=:dept_id AND salary>:sal', dept_id=50, sal=1000) When using named bind variables you can check the currently assigned ones using the bindnames() method of the cursor:
>>> print cursor.bindnames()
['DEPT_ID', 'SAL']
Passing by position is similar but you need to be careful about naming. Variable names are arbitrary so it's easy to mess up queries this way. In the example below, all three queries r1, r2, and r3 are equivalent. The parameters variable must be given as a sequence.
>>> r1 = cursor.execute('SELECT * FROM locations WHERE country_id=:1 AND city=:2', ('US', 'Seattle'))
>>> r2 = cursor.execute('SELECT * FROM locations WHERE country_id=:9 AND city=:4', ('US', 'Seattle'))
>>> r3 = cursor.execute('SELECT * FROM locations WHERE country_id=:m AND city=:0', ('US', 'Seattle'))
When binding, you can first prepare the statement and then execute None with changed parameters. Oracle will handle it as in the above case, governed by the rule that one prepare is enough when variables are bound. Any number of executions can be involved for prepared statements.
>>> cursor.prepare('SELECT * FROM jobs WHERE min_salary>:min')
>>> r = cursor.execute(None, {'min':1000})
>>> print len(cursor.fetchall())
select * from table where column in (1,2,3,4,5,6,....)
select * from table
where
column = 1
or
column = 2
or
column = 3
or
column = 4
mems = [1,2,3,4,5, ...]
q = [] # list of conditions
for c in mems:
q.append[db.table1.field == c]
all_queries_in_or = reduce(lambda a,b: (a | b ), q)
result = db(all_queries_in_or).select()
--
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/E2pVWl_71t4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Thanks for the info, Niphlod.
I will look into the efficiency of the IN clause for my needs, as it appears to offer a solution which may mollify the IT DBA, and his demands for bind vars (they are concerned that a looped select will bring the db to it's knees).
mems = [1,2,3,4,5,6,...]
for c in mems:
one_result = db(db.table.field == c).select()
......
On Friday, April 5, 2013 11:58:37 PM UTC+2, Martin Barnard wrote:
Thanks for the info, Niphlod.
I will look into the efficiency of the IN clause for my needs, as it appears to offer a solution which may mollify the IT DBA, and his demands for bind vars (they are concerned that a looped select will bring the db to it's knees).
.... a db(whatever.belongs(a_set)) issues ONE query only.
If you want to "force" a looping query, you should do explicitely with
of course, for zillions values into mems, it's not a smart move.mems = [1,2,3,4,5,6,...]
for c in mems:
one_result = db(db.table.field == c).select()
......
the smartest move with a huge set (i.e. the technique with most of the "balance") would be "paginating" through your "mems" .
You'd loop a few times but if you have thousands of values into "mems", a single IN () (or thousands ORs) will take some time ....
Try to "draw a limit" with your DBA and if he says that you're "allowed" to do an IN() with 500 values at a times, you have it covered ^_^
On the other end, you have a requirement..... fetch a zillions rows..... either you do it in one shot or in zillions/500 each.