How to escape % in a query using python's sqlalchemy's execute() and pymysql?

后端 未结 2 1295
独厮守ぢ
独厮守ぢ 2020-12-30 00:56

My query is:

result = connection.execute(
         \"select id_number from Table where string like \'_stringStart%\' limit 1;\")

gives the

2条回答
  •  悲&欢浪女
    2020-12-30 01:18

    Since this is a literal string, you're better off using a bound parameter here (illustrated using text()):

    from sqlalchemy import text
    
    connection.execute(
        text("select * from table where "
             "string like :string limit 1"), 
        string="_stringStart%")
    

提交回复
热议问题