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

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -