首页 > 解决方案 > Postgresql python插入,列关系不存在

问题描述

使用 python 从 IMDB 中抓取一些著名的电影名言并将其插入到 postgresql 数据库中。抓取数据后看起来像这样

[['The Godfather', "A man who doesn't spend time with his family can never be a real man", 'Car Wash', '25th Hour', 'Tootsie'], 
['The Usual Suspects', "The greatest trick the devil ever pulled was convincing the world he didn't exist ... and like that, he's gone", 'The Hustler', 'The Good, the Bad and the Ugly', 'Les aventures de Rabbi Jacob'].... ]

表方案

"Wrong2" character varying COLLATE pg_catalog."default" NOT NULL,
"Wrong1" character varying COLLATE pg_catalog."default" NOT NULL,
"Question" character varying COLLATE pg_catalog."default" NOT NULL,
"Id" integer NOT NULL,
"Answer" character varying COLLATE pg_catalog."default" NOT NULL,
"Wrong3" character varying COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT question_pkey PRIMARY KEY ("Id")

询问

id = None
stmt = "INSERT INTO public.question (Answer, Question, Wrong1, Wrong2, Wrong3) VALUES(%s, %s, %s, %s, %s);"
data = (databaseValueList[0][0], databaseValueList[0][1], databaseValueList[0][2], databaseValueList[0][3], databaseValueList[0][4])
cur.execute(stmt, data)
id = cur.fetchone()[0]
print(id)
conn.commit()
cur.close()

回复

column "answer" of relation "question" does not exist
LINE 1: INSERT INTO public.question (Answer, Question, Wrong1, Wrong...

我试过把它全部小写,因为它很重要(我已经读过,因为我没有使用它不应该使用的引号),转换为字符串 str(databaseValueList[0][1]) 和其他一些东西无济于事。任何关于解决它的方向的想法将不胜感激。

标签: pythonpostgresqlpsycopg2

解决方案


表中的列具有引用标识符作为名称。在查询中使用引号:

stmt = 'INSERT INTO public.question ("Answer", "Question", "Wrong1", "Wrong2", "Wrong3") VALUES(%s, %s, %s, %s, %s);'

推荐阅读