使用条件插入SQLite Python

2 投票
2 回答
1915 浏览
提问于 2025-04-15 18:31

从这篇帖子 sqlite python insert 中,我学会了如何往表里插入数据,但现在我需要用到类似“条件”的东西。

我想写的代码是这样的:cursor.execute("insert into table1(id) values (?) where ip=? and address=?",(id,),(ip,),(addr,));

2 个回答

0

在插入数据的时候,不能使用 WHERE 这个条件。如果你想为某些特定的字段指定要插入的值,应该把这些值放在值的部分:

cursor.execute("INSERT INTO table1(id, ip, address) VALUES (?, ?, ?)", (id, ip, addr))
4

INSERT 语句没有 WHERE 条件。我想你是想说 UPDATE。

撰写回答