扫码一下
查看教程更方便
sqlite 的 update 查询用于修改表中已有的记录。可以使用带有 where 子句的 update 查询来更新选定行,否则所有的行都会被更新。
带有 where 子句的 update 查询的基本语法如下:
update table_name
set column1 = value1, column2 = value2...., columnn = valuen
where [condition];
我们可以使用 and 或 or 运算符来结合 n 个数量的条件。
假设 company 表有以下记录:
id name age address salary
---------- ---------- ---------- ---------- ----------
1 paul 32 california 20000.0
2 allen 25 texas 15000.0
3 teddy 23 norway 20000.0
4 mark 25 rich-mond 65000.0
5 david 27 texas 85000.0
6 kim 22 south-hall 45000.0
7 james 24 houston 10000.0
下面示例会更新 id 为 6 的客户地址:
sqlite> update company set address = 'texas' where id = 6;
现在,company 表有以下记录:
id name age address salary
---------- ---------- ---------- ---------- ----------
1 paul 32 california 20000.0
2 allen 25 texas 15000.0
3 teddy 23 norway 20000.0
4 mark 25 rich-mond 65000.0
5 david 27 texas 85000.0
6 kim 22 texas 45000.0
7 james 24 houston 10000.0
如果想修改 company 表中 address 和 salary 列的所有值,则不需要使用 where 子句,update 查询如下:
sqlite> update company set address = 'texas', salary = 20000.00;
现在,company 表有以下记录:
id name age address salary
---------- ---------- ---------- ---------- ----------
1 paul 32 texas 20000.0
2 allen 25 texas 20000.0
3 teddy 23 texas 20000.0
4 mark 25 texas 20000.0
5 david 27 texas 20000.0
6 kim 22 texas 20000.0
7 james 24 texas 20000.0