psycopg2.ProgrammingError:关系列不存在

时间:2017-12-08 23:52:41

标签: python django python-3.x postgresql beautifulsoup

尝试将数据插入PostgreSQL数据库。

Python代码:

myFields = ((DOT_Number,),(Entity_Type,),(Operating_Status,),(Legal_Name,),
(Phone,),(Address,)
)

query = """ INSERT INTO saferdb_question( DOT_Number, Entity_Type, Operating_Status, Legal_Name, Phone, Address)VALUES ( %s, %s, %s, %s, %s, %s);"""

cur.execute( query, myFields)

获取错误:

Traceback (most recent call last):
  File "scraper.py", line 189, in <module>
    cur.execute( query, myFields)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/psycopg2/extras.py", line 144, in execute
    return super(DictCursor, self).execute(query, vars)
psycopg2.ProgrammingError: column "dot_number" of relation "saferdb_question" does not exist
LINE 1:  INSERT INTO saferdb_question( DOT_Number, Entity_Type, Oper...

创建表的PostgreSQL中的SQL:

CREATE TABLE public.saferdb_question
(
    id integer NOT NULL DEFAULT nextval('saferdb_question_id_seq'::regclass),
    "DOT_Number" character varying(10) COLLATE pg_catalog."default" NOT NULL,

...

    "Phone" character varying(15) COLLATE pg_catalog."default" NOT NULL,
    "Address" character varying(200) COLLATE pg_catalog."default" NOT NULL,

)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.saferdb_question
    OWNER to postgres;

1 个答案:

答案 0 :(得分:1)

终于解决了这个问题。 列必须用引号括起来。这是最终的查询字符串:

{{1}}