我有一个 postgres 数据库,我正在使用 psycopg2 库通过 Python 访问该数据库。我尝试插入的表如下所示:
Table "public.programmes"
Column | Type | Modifiers | Storage | Stats target | Description
-------------+-----------------------------+---------------------------------------------------------+----------+--------------+-------------
id | bigint | not null default nextval('programmes_id_seq'::regclass) | plain | |
title | character varying | not null | extended | |
start | timestamp without time zone | not null | plain | |
end | timestamp without time zone | not null | plain | |
description | character varying | not null | extended | |
genre | character varying | not null | extended | |
edited | boolean | not null | plain | |
其中id
列被创建为bigserial
到autoincrement新条目。
由于 id
列将自动递增,因此我仅尝试插入有关其余字段的数据。我将数据创建为列表并尝试像这样插入它:
def main():
postConn = psycopg2.connect("dbname=TEST host=127.0.0.1 user=user")
postCur = postConn.cursor()
prog_data = form_programme_data()
#prog_data = ['The Next Step', '2016-05-29T10:20:00', '2016-05-29T10:45:00', "2/34. My Boyfriend's Back: Reality-style drama following a group of dancers. A-Troupe meets their new choreographer and votes for open auditions for the nationals team. Also in HD. [S]", 'Lifestyle', False]
postCur.execute("""INSERT INTO programmes(title, start, end, description, genre, edited) VALUES (%s, %s, %s, %s, %s, %s);""", prog_data)
我遇到了这个错误:
Traceback (most recent call last):
File "convert_db.py", line 58, in <module>
main()
File "convert_db.py", line 32, in main
postCur.execute("""INSERT INTO programmes(title, start, end, description, genre, edited) VALUES (%s, %s, %s, %s, %s, %s);""", prog_data)
psycopg2.ProgrammingError: syntax error at or near "end"
LINE 1: INSERT INTO programmes(title, start, end, description, genre...
^
如果我尝试在不指定列名的情况下插入,它需要一个我无法触及的 id
值。它不会提示提供的前两个列名称,但由于某种原因不喜欢 end
。
感谢任何帮助。
最佳答案
显然,end
是 key word在 PostgreSQL 中。您需要引用它才能将其解释为标识符:
postCur.execute("""INSERT INTO programmes
(title, start, "end", description, genre, edited)
VALUES (%s, %s, %s, %s, %s, %s);""", prog_data)
关于python - psycopg2 无法插入特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926717/