cur.execute('SELECT COUNT(*) FROM persons WHERE id=?;', (id,))
if cur.fetchone()[0] == 0:
# Record does not exist.
cur.execute('INSERT INTO persons (id, username, points, score) VALUES (?,?,?,?);', (id,username,points,score)
else:
# Record exists.
cur.execute('UPDATE persons SET points=points+?, score=score+? WHERE id=?;', (points,score,id))
2条答案
按热度按时间ldioqlga1#
这就像在Python中更新字典一样。如果记录不存在,则创建它,否则更新它。
字符串
c2e8gylq2#
自版本3.24.0起,SQLite支持
UPSERT
:字符串
假设
id
是唯一的,这将起作用。参见demo。