database - Web2py: Write SQL query: "SELECT x FROM y" using DAL, when x and y are variables, and convert the results to a list? -
my action passes list of values column x
in table y
view. how write following sql: select x y
, using dal "language", when x
, y
variables given view. here is, using exequtesql()
.
def myaction(): x = request.args(0, cast=str) y = request.args(1, cast=str) myrows = db.executesql('select '+ x + ' '+ y) #let's convert list: mylist = [] row in myrows: value = row #this line doesn't work mylist.append(value) return (mylist=mylist)
also, there more convenient way convert data list?
first, note must create table definitions tables want access (i.e., db.define_table('mytable', ...)
). assuming have done , y
name of single table , x
name of single field in table, do:
myrows = db().select(db[y][x]) mylist = [r[x] r in myrows]
note, if records returned, .select()
produces row
object, comprises set of row
objects (even if single field selected). so, extract individual values list, have iterate on rows
object , extract relevant field each row
object. above code via list comprehension.
also, might want add code check whether db[y]
, db[y][x]
exist.
Comments
Post a Comment