加入,更新和删除数据

Inserting data

The INSERT statement is used to insert data into tables. We will create a new table in which to execute our examples.

sqlite> CREATE TABLE Books(Id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, 
   ...> ISBN TEXT DEFAULT 'not available');

We create a new table Books, with Id, Title, Author and ISBN columns.

sqlite> INSERT INTO Books(Id, Title, Author, ISBN)
   ...> VALUES(1, 'War and Peace', 'Leo Tolstoy', '978-0345472403');

This is the classic INSERT SQL statement. We have specified all column names after the table name and all values after the VALUES keyword. We add our first row into the table.

sqlite> INSERT INTO Books(Title, Author, ISBN)
   ...> VALUES('The Brothers Karamazov', 'Fyodor Dostoyevsky', '978-0486437910');

We add a new title into the Books table. We have omitted the Id column. The Id column is defined as INTEGER PRIMARY KEY. Such columns are auto-incremented in SQLite. This means the SQLite library will add a new Id.

sqlite> SELECT * FROM Books;
Id|Title|Author|ISBN
1|War and Peace|Leo Tolstoy|978-0345472403
2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910

Here is what we have in the Books table.

sqlite> INSERT INTO Books VALUES(3, 'Crime and Punishment', 'Fyodor Dostoevsky',
   ...> '978-1840224306');

In this SQL statement, we did not specify any column names after the table name. In such a case, we have to supply all values.

sqlite> .nullvalue NULL

The .nullvalue command tells the SQLite to show NULL values as NULL. SQLite shows empty strings for NULL values by default.

sqlite> INSERT INTO Books(Id, Title) VALUES(4, 'Paradise Lost');

The INSERT statement omits the last 2 columns. Such columns are filled with the default value, or NULL if there is no default value. The Author column does not have a default value, so there is a NULL value. In the CREATE TABLE statement, we have specified the ISBN column to have the 'not available' default value.

sqlite> SELECT * FROM Books WHERE Id=4;
Id|Title|Author|ISBN
4|Paradise Lost|NULL|not available

In the third column we have a NULL value. The fourth has the default 'not available' string.

sqlite> INSERT INTO Books VALUES(4, 'Paradise Lost', 'John Milton', '978-0486442877');
Error: PRIMARY KEY must be unique
sqlite> INSERT OR REPLACE INTO Books VALUES(4, 'Paradise Lost', 'John Milton', 
   ...> '978-0486442877');

Say we want to put all information into the fourth column. Trying to insert new data into existing row produces the following error: 'PRIMARY KEY must be unique'. In such a case we can use the INSERT OR REPLACE statement. The same could be accomplished with the UPDATE statement.

sqlite> SELECT * FROM Books WHERE Id=4;
Id          Title          Author       ISBN          
----------  -------------  -----------  --------------
4           Paradise Lost  John Milton  978-0486442877

Now we have all information in the fourth row.


Since SQLite version 3.7.11 it is possible to insert multiple rows using one INSERT statement.

sqlite> CREATE TABLE Ints(Id INTEGER PRIMARY KEY, Val INTEGER);

We will use a one-column Ints table to show a multi-row INSERT statement. The table's lone column stores integers.

sqlite> INSERT INTO Ints(Val) VALUES (1), (3), (5), (6), (7), (8), (6), (4), (9);

We insert nine rows into the table in one shot. The rows follow the VALUES keyword and are separated by a comma character.

sqlite> SELECT * FROM Ints;
Id          Val       
----------  ----------
1           1         
2           3         
3           5         
4           6         
5           7         
6           8         
7           6         
8           4         
9           9  

These are the contents of the Ints table.


We can use the INSERT and SELECT statements together in one statement.

sqlite> CREATE TABLE Books2(Id INTEGER PRIMARY KEY, Title TEXT, 
   ...> Author TEXT, ISBN TEXT);

First, we create a new table called Books2. Its schema is identical to Books table.

sqlite> INSERT INTO Books2 SELECT * FROM Books;

Here we insert all data from the Books table into the Books2 table.

sqlite> SELECT * FROM Books2;
Id|Title|Author|ISBN
1|War and Peace|Leo Tolstoy|978-0345472403
2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910
3|Crime and Punishment|Fyodor Dostoevsky|978-1840224306
4|Paradise Lost|NULL|not available

We verify it. All is OK.

Deleting data

The DELETE keyword is used to delete data from tables. First, we are going to delete one row from a table. We will use the Books2 table, which we have created previously.

sqlite> DELETE FROM Books2 WHERE Id=1;

We delete a row with Id 1.

sqlite> SELECT * FROM Books2;
Id|Title|Author|ISBN
2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910
3|Crime and Punishment|Fyodor Dostoevsky|978-1840224306
4|Paradise Lost|NULL|not available

We verify that the first row is missing.

sqlite> DELETE FROM Books2;

This SQL statement deletes all data in the table.

Updating data

The UPDATE statement is used to change the value of columns in selected rows of a table.

Say we wanted to change 'Leo Tolstoy' to 'Lev Nikolayevich Tolstoy' in our Books table. The following statement shows how to accomplish this.

sqlite> UPDATE Books SET Author='Lev Nikolayevich Tolstoy' WHERE Id=1;

The SQL statement sets the author column to 'Lev Nikolayevich Tolstoy' for the column with id=1.

sqlite> SELECT * FROM Books WHERE Id=1;
Id|Title|Author|ISBN
1|War and Peace|Lev Nikolayevich Tolstoy|978-0345472403

The row is correctly updated.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值