0

Ok this one should be an easy question id imagine, but cannot figure it out.

Im passing form data to the controller and attempting to do a data search there, which in turn, runs this..

def initLogin():
    userName = request.vars.user_name;
    counter = db(db.Users.UserName == userName).count()
    if counter > 0:
        return DIV("User exists")

return DIV("user does not exist")

I have checked the value is being passed correctly( which is is) by returning userName instead of the string, which shown me it was the correct value, and when i had a direct string of a correct username, it seemed to work. So my question is.. how do you run a count() function with web2py databases correctly using variables?

1
  • The above code is correct. If the if counter > 0 statement is not evaluating to True, then the record in question was not found. The problem must lie elsewhere. What happens if you replace userName with the exact string you are passing in request.vars.user_name? Commented Oct 19, 2014 at 14:54

1 Answer 1

1

Your code is correct and shouldn't be giving you any problems, the only possible problem should be in your userName var not being what you expected or an incorrect sql query. I recommend that you try changing your controller to:

def initLogin():
    userName = request.vars.user_name;
    counter = db(db.Users.UserName == userName).count()
    lastExecutedQuery = db._lastsql

    return DIV( lastExecutedQuery )

And check if the query being performed is the one that you expected.

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.