我正在尝试执行以下命令,为代码列等于“code”的行设置“store”=“result”。
cursor.execute("""UPDATE pjStores SET %s=%s WHERE code=%s""", (store, result, code))
我仍然收到以下错误:
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1087'=1 WHERE code='Madness16'' at line 1")
命令中的变量是:
store=1087
result=1
code=Madness16
这是我第一次真正使用mysql,所以我是新手。我现在已经被困在这条线上约2个小时了,无法弄清楚我做错了什么。我在mysql中尝试了以下命令,它运行正常:
UPDATE pjStores SET `1087`=1 WHERE code='Madness16'
根据要求提供更多代码:
# Step through stores
cursor.execute("SHOW COLUMNS FROM pjStores")
stores = cursor.fetchall()
cursor.execute("SELECT code FROM pjStores")
codes = cursor.fetchall()
for store in stores[1:]: # Hack to skip first entry
pj.setStore(store[0])
for code in codes:
result = pj.checkCode(code[0])
cursor.execute ("""UPDATE pjStores SET %d=%s WHERE code=%s""", (store[0], result, code[0]))
答案 0 :(得分:2)
你可能想尝试这样的事情,(假设这不容易受到sql注入攻击 - 意味着数据是可信的而不是用户提供的)
...
for code in code:
result = pj.checkCode(code[0])
query = """UPDATE pjStores SET `%s` = %%s WHERE `code` = %%s""" % store[0]
cursor.execute(query, (result, code[0]))
答案 1 :(得分:0)
尝试使用'
代替"
:
cursor.execute('''UPDATE pjStores SET %s=%s WHERE code=%s''', (store, result, code))