psycopg2.ProgrammingError: column "none" does not exist - Update Statement

psycopg2.ProgrammingError: column "none" does not exist - Update Statement

我一直遇到错误"psycopg2.ProgrammingError: column "none“不存在”。

我已阅读 psycopg2 documentation 并查看了有关此错误的其他帖子,但是,许多帖子特定于 insert query 而不是 更新查询.

我的代码如下:-

def send_to_sql(identity,list1,list2,list3):
    userid = 30
    Fixture_No = None
    Home_Side_Score = None
    Away_Side_Score = None
    conn = psycopg2.connect("dbname='Euro2016' user='postgres' host='localhost' password='##passwordhere##'")
    cur = conn.cursor()
    cur.execute("""UPDATE "Predictions" SET userid = %r, "Fixture_No" = %r, "Home_Side_Score" = %r, "Away_Side_Score" = %r WHERE "Predictions"."userid" = %r AND "Predictions"."Fixture_No" = %r """ % (userid, Fixture_No, Home_Side_Score, Away_Side_Score, userid, Fixture_No))
    conn.commit()
    cur.close()
    conn.close()

如果我尝试传递的分数和赛程编号是数字,则此代码可以正常工作,但是当输入 None 时,代码会掉下来。有人救救我...

使用 %s 而不是 %r。要检查发送到服务器的内容,请使用 cursor. mogrify:

update = """
    UPDATE "Predictions"
    SET 
        userid = %s, 
        "Fixture_No" = %s, 
        "Home_Side_Score" = %s, 
        "Away_Side_Score" = %s
    WHERE 
        "Predictions"."userid" = %s AND 
        "Predictions"."Fixture_No" = %s
"""
data = (userid, Fixture_No, Home_Side_Score, Away_Side_Score, userid, Fixture_No)
print cur.mogrify(update, data)
cur.execute(update, data)

更好,使用字典传递数据:

update = """
    UPDATE "Predictions"
    SET
        userid = %(userid)s, 
        "Fixture_No" = %(Fixture_no)s,
        "Home_Side_Score" = %(Home_Side_Score)s,
        "Away_Side_Score" = %(Away_Side_Score)s
    WHERE
        "Predictions"."userid" = %(userid)s AND
        "Predictions"."Fixture_No" = %(Fixture_No)s
"""
data = dict (
    userid = userid,
    Fixture_No = Fixture_No,
    Home_Side_Score = Home_Side_Score,
    Away_Side_Score = Away_Side_Score
)