Psycopg2 Postgres 插入单引号文本时出现问题

问题描述 投票:0回答:1

我在使用 psycogp2 将带单引号的文本插入 Postgres DB 时遇到问题。这是每当遇到带单引号的文本时出现的错误。

psycopg2.errors.UndefinedColumn: column "dept of health (department), in consul" does not exist

当我删除文本中的单引号时..记录已插入,没有任何问题。

这是我的查询:

query = "INSERT INTO %s VALUES %s ON CONFLICT (id) DO UPDATE SET %s;" % (name, tuple(row), ', '
                                                                                            ''.join(update_string))

我已经尝试过此链接中提到的解决方案,例如用双引号替换单引号并转义单引号,但仍然存在问题。

python postgresql psycopg2
1个回答
0
投票

如果您在数据中遇到单引号并且给您带来麻烦,您可以在查询之前使用 string.replace("'","''") 。

在数据库中使用单引号,'' 基本上被识别为 '

© www.soinside.com 2019 - 2024. All rights reserved.