python - Postgresql python插入,列关系不存在

标签 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]) 和结合其他事情无济于事。任何关于找出它的方向的想法将不胜感激。

最佳答案

您表中的列有 quoted identifiers作为名字。在查询中使用引号:

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

关于python - Postgresql python插入,列关系不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56939866/

相关文章:

python - Linux 中的 Tkinter 外观(主题)

Python:理解对象 __del__ 方法

python - 向字典键添加值

python - 处理 psycopg2 中的错误 - 一个错误似乎会产生更多错误?

Python 或 WPF 报告应用程序

python - 使用 SQLAlchemy 删除没有相关记录的行

sql - 如何从具有 MAX 值的行中选择其他字段

ruby-on-rails - Postgresql 和 Texticle 的架构不存在错误

python - cursor.copy_from 在 postgres 中失败

python - psycopg2 + 存储过程 + 复合类型