1

I'm using sqlalchemy as a drop in replacement to MySQLdb, that means I'm using session.execute rather than using mappers.

However I've run into a reproducible error, namely an ValueError: "incomplete format" exception wherever I use "%" in a query, this is a problem since the "%" character is necessary for date formatting of UNIX_TIMESTAMP and the LIKE statement in MySQL.

I have tried using "\%", "\\%" and "%%" without luck.

1
  • 3
    a code sample would be quite helpful Commented Dec 13, 2011 at 21:47

1 Answer 1

3

In concrete case you can use the methods:contains(), startswith(), endswith()

For illustrate how use it:

session.query(Users).filter(User.full_name.startswith('Mr.%s' % first_name))) 
# => It is equivalent to requesting full_name LIKE 'Mr.user1%'

session.query(Users).filter(User.full_name.endswith('%s.' % last_name))) 
# => It is equivalent to requesting full_name LIKE '%user1.'

session.query(Users).filter(User.full_name.contains('%s' % middle_name))) 
# => It is equivalent to requesting full_name LIKE '%user1%'
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.