gpt4 book ai didi

python - psycopg2 无法插入特定列

转载 作者:行者123 更新时间:2023-11-29 12:03:56 24 4
gpt4 key购买 nike

我有一个 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 列创建为 autoincrementbigserial新条目。

由于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com