SQLite 不接受唯一命令

SQLite not accepting unique command

Python/Flask:

cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS users (email TEXT NOT NULL UNIQUE, password TEXT)')
print('Table created')

然后换个方法

def create_user(email, hashedpw):
    try:
        cur.execute('INSERT INTO users VALUES (?, ?)', (email, hashedpw))
        conn.commit()
        return "works"
    except:
        print(str(sqlite3.Error))
        return None

即使我输入同一个邮件10次它仍然会在数据库中记录数据并且不会报错。我明明设置为 UNIQUE 那为什么还不行呢?

您的语法看起来不错,所以想到的唯一解释是:

您的数据库中已经有一个名为 users 的 table,它没有 UNIQUE 约束,并且由于您使用的是 IF NOT EXISTS,因此 table 保持原样。