使用Python、Flask、psycopg2进行的令人困惑的编码不一致性

2024-07-04 08:55:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用postgres和psycopg2 python/flask web应用程序。在

我有一个编码问题,我肯定是一些愚蠢的东西我错过了(我是新编程)。下面的语句非常有效:

cur.execute("SELECT column_name FROM information_schema.columns where table_name = %s;", (tablename,))

我使用fetchall()在表中创建列名列表。然而,另一种说法行不通:

^{pr2}$

错误如下:

psycopg2.ProgrammingError
ProgrammingError: syntax error at or near "E'flatresponses_1'"
LINE 1: ALTER TABLE E'flatresponses_1' ADD COLUMN E'What was the bes...

('flatresponses_1'是'tablename','What was best…'是'col'的开头。)

我确实打印了当前查询“结果如下:

>>> print cur.query
>>> ALTER TABLE E'flatresponses_1' ADD COLUMN E'What was the best part of your ENT clinic visit today? Why?' varchar;

我在第二个查询中得到了E'编码,但不是第一个查询。我也试过str(tablename)。在

我错过了什么?!在


Tags: thenameadd编码tablecolumnwhatpsycopg2

热门问题