Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My query is:

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

gives the error:

query = query % escaped_args TypeError: not enough arguments for format string 

A quick google said to use %% instead of % but that doesn't work either. How do I escape the % or is there another way to query for a string that starts with a random letter then a certain sequence?

like image 243
user713713 Avatar asked Jun 07 '12 16:06

user713713


1 Answers

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%") 
like image 171
zzzeek Avatar answered Sep 28 '22 02:09

zzzeek