psycopg2.errors.UndefinedColumn

问题描述 投票:0回答:1

我使用下面的代码在postgresql中插入一行:

    query = "INSERT INTO pages_questions (text , c1 , c2 , c3 , c4 , correct_choice_Id) VALUES(\"{}\",\"{}\",\"{}\",\"{}\",\"{}\", {})".format(text, c1, c2, c3, c4, correct)
    curs.execute(query) 
    conn.commit()

但是它给了我这个错误:

psycopg2.errors.UndefinedColumn:“”的“ correct_choice_id”列关系“ pages_questions”不存在第1行:... INTOpages_questions(文字,c1,c2,c3,c4,correct_ch ...^

我尝试了许多解决方案,但没有一个起作用。我的代码有什么问题?

python postgresql psycopg2
1个回答
0
投票

尝试一下:

data = [('text', 'BLAH BLAH'), ('c1', 'False'), ('c2', 'True'),('c3', 'True'),('C4', 'NuLL') ,('correct_choice_Id',12)]
data = dict(data)
cols = ",".join(data.keys())
values = ",".join("'"+ v + "'" if type(v) is str else str(v) for v in data.values())
cur.execute("INSERT INTO pages_questions (%s) VALUES (%s)" % (cols,values))
© www.soinside.com 2019 - 2024. All rights reserved.