给出SQLite表
CREATE TABLE users (
username TEXT NOT NULL,
hashed_password TEXT NOT NULL,
disabled INTEGER DEFAULT 1,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE(username)
) ;
和查询
Q = {
"sqlite": {
"upsert_user": """ INSERT INTO users(username, hashed_password, disabled, updated_at) VALUES(?,?,?,CURRENT_TIMESTAMP) ON CONFLICT(username) DO UPDATE SET hashed_password=?, disabled=?, updated_at=CURRENT_TIMESTAMP """
}
}
和描述用户的元组
user = ('awesome-username',
'$2b$12$55Nkrpw2Gz3p4yg8gD8XS.EmLOOdE5W3Hj3rkzmAsMrAYMm.swwfq',
0,
'$2b$12$55Nkrpw2Gz3p4yg8gD8XS.EmLOOdE5W3Hj3rkzmAsMrAYMm.swwfq',
0)
当我尝试向上插入/插入user
with db_conn as conn:
conn.execute(
Q["sqlite"]["upsert_user"],
(
username,
hashed_password,
0,
hashed_password,
0,
)
)
失败并显示错误
Traceback (most recent call last):
File "<ipython-input-13-63b5a41b8c7f>", line 43, in is_user_identity_managed
1
sqlite3.OperationalError: near "ON": syntax error
请注意,我将原始查询粘贴到SQLite中,成功了。
我的头脑将爆炸,弄清楚为什么以上代码在Python中失败,请您指教吗?