Skip to content Skip to sidebar Skip to footer

Python 2.7 To Python 3.4 Error Unsupported Operand Type(s) For %: 'bytes' And 'dict'

I got the following code from How do I get a raw, compiled SQL query from a SQLAlchemy expression? and it worked fine until we moved from Python 2.7 to Python 3.4. I've made a few

Solution 1:

Thanks to the comments, I ported it to python 3

def compile_query(query):
    dialect = query.session.bind.dialect
    statement = query.statement
    comp = compiler.SQLCompiler(dialect, statement)
    comp.compile()
    enc = dialect.encoding
    params = {}
    for k,v in comp.params.items():
        if isinstance(v, str):
            v = v.encode(enc)
        params[k] = sqlescape(v)
    return (comp.string % params)

Post a Comment for "Python 2.7 To Python 3.4 Error Unsupported Operand Type(s) For %: 'bytes' And 'dict'"