我正在使用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)。在
我错过了什么?!在
最后我使用了asispsycopg2扩展,如this post中所述。工作得很有魅力!在
表名和列名不是文本类型,而是标识符(type=name)。它们不接受转义字符串字面值,并且您可能需要%s以外的内容作为占位符。在
http://www.postgresql.org/docs/9.2/static/datatype-character.html
相关问题 更多 >
编程相关推荐