python - Getting first row from sqlalchemy -


i have following query:

profiles = session.query(profile.name).filter(and_(profile.email == email, profile.password == password_hash)) 

how check if there row , how return first (should 1 if there match)?

use query.one() one, , exactly 1 result. in other cases raise exception can handle:

from sqlalchemy.orm.exc import noresultfound sqlalchemy.orm.exc import multipleresultsfound  try:     user = session.query(user).one() except multipleresultsfound, e:     print e     # deal except noresultfound, e:     print e     # deal 

there's query.first(), give first result of possibly many, without raising exceptions. since want deal case of there being no result or more thought, query.one() should use.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -