It worked!!!! Oh my sweet Jesus it worked!!!!  

I have scoured the book, and looked at dozens of posts but have yet to come 
across passing a variable into a query.  I have been parsing the data that 
has been coming from the database to isolate the data I wanted instead of 
all of the junk that comes with the row.  Thank you so much!!!

Here's what you have done for me.  I did have this:

def db_query(db_name, db_field, length):
    a = '(db.' + db_name + '.Financial_Institution==auth.user.id)&(db.' + 
db_name + '.Completed==1)'
    query = eval(a)
    b = 'db(query).select(' + db_field + ')'
    answer = eval(b)
    answer = str(answer[0])
    ending = (len(answer)) - 4
    answer = answer[length:ending]
    return answer

This worked, but it was all wrong in my approach.  Now, I have this!

def db_query(db_name, db_field, length):
    query = (db[db_name].Financial_Institution == auth.user.id) & 
(db[db_name].Completed == 1)
    return db(query).select(db[db_name][db_field]).first()[db_field]

Oh my goodness, I could kiss you!

Thanks,
Travis May

On Saturday, March 1, 2014 7:29:17 AM UTC-6, Anthony wrote:
>
> Assuming by db_name you really mean the table name, maybe you want 
> something like this:
>
> def db_query(tablename, fieldname):
>     query = (db[tablename].Financial_Institution == auth.user.id) & (db[
> tablename].Completed == 1)
>     return db(query).select(db[tablename][fieldname]).first()[fieldname]
>
> Do you really have multiple tables with Finacial_Institution and Completed 
> fields, and do you really need to do the same query on these tables, except 
> for returning different field values? Otherwise, it's not clear how useful 
> this function is?
>
> Note, if you have a DAL table or field name in a variable, you can use 
> db[tablename][fieldname] and row[fieldname] syntax rather than 
> db.tablename.fieldname and row.fieldname (the latter syntax using dot 
> separators represents attribute retrieval and requires the literal 
> attribute names, not variables holding the names).
>
> Anthony
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to