开发者

how to create literal based query in sqlalchemy?

开发者 https://www.devze.com 2023-04-04 13:12 出处:网络
I created one function for create expression def test(operator1, operation, operator2): return literal_column(operator1).op(operation)(operator2)

I created one function for create expression

def test(operator1, operation, operator2):
    return literal_column(operator1).op(operation)(operator2)

Now when I call it with

test(1, '=', 1)

then it works

But when I pass

test('abc', '=', 'abc')

Then it gives error that abc is not a column.

开发者_高级运维

I tried to convert it

def test(operator1, operation, operator2):
    return literal_column(operator1, String).op(operation)(operator2)

But that was not working.

This will work if i call with

test("'abc'", '=', 'abc')

Is there any way, to get the type of operator1 and on that bases we can create literal_colum which will be map to same type of content ?


any literal value can be converted to an expression construct:

from sqlalchemy import literal_column, bindparam


# ? = ?, 1 will be bound
bindparam(1) == bindparam(1)

# " 1 = 1", literals rendered inline (no quoting is applied !!)
literal_column(str(1)) == literal_column(str(1))
0

精彩评论

暂无评论...
验证码 换一张
取 消