【发布时间】:2018-05-26 14:14:46
【问题描述】:
我在使用 python、sqlite3 时遇到问题,我可以用数字(整数或十进制)而不是字符串更新表。 link to a screenshot of program running here
def amendRec(): #FIX ME PLEASE
edit="yes"
while edit !='exit':
keyfield=input("enter the game name of the record you want to edit. ")
keyfield="'"+keyfield+"'"
field=input("enter the field you want to change. ")
newVal=input("enter the new data for this field. ")
#try:
cursor.execute("UPDATE tblGames SET " + field + "=" + newVal + " WHERE game = " + keyfield)
conn.commit()
print("\nRecord Updated\n")
#except:
#print("invalid game name or field entered.")
edit=input("type 'exit' to stop editing, or press enter to continue. \n")
showtable()
except 语句被注释掉,所以我可以看到 try 语句的错误消息。同样,当我尝试将 enrty 更新为数字时,它工作得很好,但是当我尝试更新为字符串时,它会给出错误消息:
Traceback(最近一次调用最后一次): 文件“C:/Users/12smi/Documents/school/computing/SQL/database with add and remove function.py”,第 99 行,在 修正记录() 文件“C:/Users/12smi/Documents/school/computing/SQL/database with add and remove function.py”,第 82 行,在 amendRec cursor.execute("UPDATE tblGames SET" + field + "=" + newVal +" WHERE game = " + keyfield) sqlite3.OperationalError:没有这样的列:冒险
('Adventure'是我当时输入的字符串)
我不知道为什么会这样,所以任何帮助都会很棒:)
【问题讨论】:
-
我真的无法跟上查询字符串格式的斗争。请不要使用它,这是一个很大的 SQL 注入风险,它会导致代码变慢且不清晰。至于你的错误,我们看不到表,所以大概
Adventure不存在于架构中 -
"UPDATE tblGames SET {} = ? WHERE game = ?".format(field), (newfield, newVal))。但是由于用户输入,这确实很容易出错并且仍然不安全。