和SQL Server数据库一样,SQLite数据库的修改和删除数据也是通过UPDATE命令和DELETE命令来实现的,本文通过实例代码来介绍这一过程,接下来就让我们开始学习吧。
修改数据 UPDATE 表 SET 列 = '新值' 【WHERE 条件语句】
UPDATE 语句用来更新表中的某个列,如果不设定条件,则所有记录的这一列都被更新; 如果设定了条件,则符合条件的记录的这一列被更新, WHERE 子句被用来设定条件,如下例:
- sqlite>
- sqlite> SELECT * FROM Teachers;
- Id Name Age Country
- ---- --------------- --------------- ---------------
- 1 Alice 25 CHN
- 2 Bob 25 BRA
- 3 Charls 33 USA
- 4 Jhon 43 USA
- sqlite>
- sqlite>
- sqlite> UPDATE Teachers SET Country='China';
- sqlite> SELECT * FROM Teachers;
- Id Name Age Country
- ---- --------------- --------------- ---------------
- 1 Alice 25 China
- 2 Bob 25 China
- 3 Charls 33 China
- 4 Jhon 43 China
- sqlite>
- sqlite>
- sqlite> UPDATE Teachers SET Country='America' WHERE Id=3;
- sqlite> SELECT * FROM Teachers;
- Id Name Age Country
- ---- --------------- --------------- ---------------
- 1 Alice 25 China
- 2 Bob 25 China
- 3 Charls 33 America
- 4 Jhon 43 China
- sqlite>
- sqlite>
- sqlite> UPDATE Teachers SET Country='India' WHERE Age<30;
- sqlite> SELECT * FROM Teachers;
- Id Name Age Country
- ---- --------------- --------------- ---------------
- 1 Alice 25 India
- 2 Bob 25 India
- 3 Charls 33 America
- 4 Jhon 43 China
- sqlite>
删除数据 DELETE FROM 表 【WHERE 条件语句】
如果设定 WHERE 条件子句,则删除符合条件的数据记录;如果没有设定条件语句,则删除所有记录。
- sqlite>
- sqlite> SELECT * FROM Teachers;
- Id Name Age Country
- ---- --------------- --------------- ---------------
- 1 Alice 25 India
- 2 Bob 25 India
- 3 Charls 33 America
- 4 Jhon 43 China
- sqlite>
- sqlite>
- sqlite> DELETE FROM Teachers WHERE Age>30;
- sqlite> SELECT * FROM Teachers;
- Id Name Age Country
- ---- --------------- --------------- ---------------
- 1 Alice 25 India
- 2 Bob 25 India
- sqlite>
- sqlite>
- sqlite> DELETE FROM Teachers;
- sqlite> SELECT * FROM Teachers;
- sqlite>
关于SQLite数据库修改和删除数据库的知识就介绍到这里,希望能对各位有所帮助。
【编辑推荐】