Skip to content Skip to sidebar Skip to footer

Apostrophe In Select With Postgres/python

I am trying to see if I already have an item in the database. However I can't figure out how to select the name with an apostrophe(') in them. I have tried using adapt, %s (which d

Solution 1:

Rather than using (f-) string formatting, use the DB-API's parameter substitution method:

str_sql = text("select * from items where name = %s;")
results = self.conn.execute(str_sql, (item_name,)).fetchone()

Note that the value(s) must be passed as a sequence; specifically a tuple or a list. A string is a sequence, but each character in the string will be interpreted as a distinct value for the query, which will result in

TypeError: not all arguments converted during string formatting

being raised.

Post a Comment for "Apostrophe In Select With Postgres/python"