0

I have this function (SQLAlchemy):

# table: Table to search (should be the class name)
# column: filter to use
# search: search term
#
# Returns a list [object] with the found entries in the database
# Uses the SQL LIKE statement

def search(table, column, search):
    results = dbsession.query(table).filter(column.like('%' + search + '%')).all()
    return results

So this search function searches in the class 'table', uses the filter 'column' and searches for 'search'. The problem I'm having now is that if I enter a value for 'column' (wich is actually not a string but a piece of code) I always get an error that the name doesn't exists:

  users =  search(User, fname, 'Jo')
NameError: name 'fname' is not defined

Does anybody know how to code this properly?

3
  • 3
    That is a runtime error because you did not define fname, not a sqlalchemy problem. Commented Mar 7, 2013 at 11:26
  • alright, but how should I do it then? I want to search in the fname column? Commented Mar 7, 2013 at 11:26
  • If you have the table object, why not just grab it from there? User.fname is the column.. Commented Mar 7, 2013 at 11:28

1 Answer 1

1

Use users = search(User, User.fname, 'Jo') instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.