用户输入用户ID,然后对“是”或“否”进行投票,但是代码返回true,即使用户未投票也执行if语句。
例如-做userid = 1并投票,userid = 1并投票。 userid第二次尝试投票时确实表示您已投票,但是如果您在第三次迭代中将userid = 2表示您已投票,则不正确。
import sqlite3
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
with conn:
cur.execute("CREATE TABLE users(votes, userid INTEGER)")
for i in range(3):
userid = input("Enter your user id: ")
votes = input("Vote yes or no: ")
with conn:
cur.execute("SELECT * FROM users WHERE votes = 'yes' OR 'no' AND userid = ?", (userid,))
user_voted = cur.fetchall()
if user_voted:
print("You have already voted")
else:
cur.execute("INSERT into users(userid, votes) VALUES(?, ?)", (userid, votes))
print(cur.fetchall())
with conn:
cur.execute("SELECT * FROM users")
print(cur.fetchall())
没有错误消息,只会产生错误的结果
答案 0 :(得分:1)
WHERE票='是'或'否'
如果您不想重复提及votes
列,
然后将其表达为:
WHERE votes in ('yes', 'no')