【问题标题】:Postgresql python insert, column relation does not existPostgresql python插入,列关系不存在
【发布时间】:2019-11-18 06:15:24
【问题描述】:

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

【问题讨论】:

    标签: python postgresql psycopg2


    【解决方案1】:

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

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

    【讨论】:

    • 这似乎完成了,我可以发誓我已经尝试过,因为我阅读了几篇关于同一问题的帖子。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多