PostgreSQL python插入,列关系不存在

时间:2019-07-08 17:25:52

标签: python postgresql psycopg2

使用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])和一个夫妇其他事情无济于事。关于解决该问题的任何方向的任何想法将不胜感激。

1 个答案:

答案 0 :(得分:1)

表中的列以quoted identifiers作为名称。在查询中使用引号:

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