rgz
rgz

Reputation: 362

sqlalchemy "incomplete format" problems

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.

Upvotes: 1

Views: 1059

Answers (1)

Nikolay Moskvin
Nikolay Moskvin

Reputation: 1048

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%'

Upvotes: 3

Related Questions