python - psycopg2 无法插入特定列

标签 python postgresql

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

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

感谢任何帮助。

最佳答案

显然,endkey 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/

相关文章:

java - 无法在 Spring Boot 中插入多对多关系的记录

ruby-on-rails - 编辑插入新记录

python - 删除 pandas df 中的特定值和后续行

python - 我不明白有两个输入的 map_fn

java - Solr 选择只返回 id

postgresql - plpgsql For循环中的Select语句创建多个CSV文件

python - 从 Google Colab 连接到 Postgresql 数据库

python - Geopandas - ValueError : Cannot transform naive geometries. 请先在对象上设置 crs

python - 缺少snakemake中所有规则的输入文件

python - python中的多文档摘要