我一直遇到错误“psycopg2.ProgrammingError:column”none“不存在”。
我已阅读psycopg2 documentation并查看了有关此错误的其他帖子,但许多内容仅针对插入查询而非更新查询。
我的代码如下: -
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()
如果我试图传递的分数和夹具数是数字,但是当无条目输入时,代码就会正常工作。有人救了我......
答案 0 :(得分:2)
使用%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
)