使用postgres/Python在select中使用撇号

使用postgres/Python在select中使用撇号,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我正在尝试查看数据库中是否已有项。但是,我不知道如何选择带有撇号(')的名称 我已尝试使用自适应%s(根本不起作用):项目语法也不起作用 下面的选择将导致 Exception has occurred: ProgrammingError (psycopg2.errors.SyntaxError) syntax error at or near "s" LINE 1: select * from items where name = 'Akunda's Bite' # COD

我正在尝试查看数据库中是否已有项。但是,我不知道如何选择带有撇号(')的名称

我已尝试使用自适应%s(根本不起作用):项目语法也不起作用

下面的选择将导致

Exception has occurred: ProgrammingError
(psycopg2.errors.SyntaxError) syntax error at or near "s"
LINE 1: select * from items where name = 'Akunda's Bite'

# CODE
str_sql = text(f"select * from items where name = '{item_name}'")

results = self.conn.execute(str_sql).fetchone()
不要使用(f-)字符串格式,而是使用DB-API的:


请注意,值必须作为元组传递。

这不需要元组。str_sql=“select*from items where name=%s”results=self.conn.execute(str_sql,item_name).fetchone()然后
item_name
必须是一个元组或列表。如果我将字符串“Akunda's Bite”作为第二个参数传递给
execute
,我会得到
TypeError:在字符串格式化过程中并非所有参数都被转换。
str_sql = text("select * from items where name = %s;")
results = self.conn.execute(str_sql, (item_name,)).fetchone()