SQLAlchemy 0.7.8 raw string queries issue with params

pyramid, python, sqlalchemy

Solution

By using the `execute` method of the session, the parameters handling is done by SQLAlchemy, thus you need something like:

DBSession.execute('SELECT id FROM users WHERE email = :email;', {'email': email})

However, if you use the `execute` method of the engine instead, then the parameters handling is done by the underlying DB-API, which is pg8000 in this case, e.g.

DBSession.bind.execute('SELECT id FROM users WHERE email = %s;', email)

Anyway, I always stick with the former, to have a consistent way to handle parameters across different drivers / databases.

Problem

I'm using SQLAlchemy with Pyramid application and I need to use 'format' paramstyle. For example: ``` DBSession.execute('SELECT id FROM users WHERE email = %s;', email) ``` This in theory should work, but I get sqla error: ``` ('SQL Error!', AttributeError("'list' object has no attribute 'keys'",)) ``` even while `email` is string and not list. I tried using tuple but I get the same error (but instead of "list" it says "tuple"). I use `pg8000` as DB driver. How else should I supply params to the query?

Original source