SQLite: Insert Data

From OnnoWiki
Revision as of 07:22, 7 December 2013 by Onnowpurbo (talk | contribs) (New page: Inserting, updating and deleting data In this part of the SQLite tutorial, we will insert, update and delete data from SQLite tables. We will use the INSERT, DELETE and UPDATE statements....)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Inserting, updating and deleting data

In this part of the SQLite tutorial, we will insert, update and delete data from SQLite tables. We will use the INSERT, DELETE and UPDATE statements. These statements are part of the SQL Data Manipulation Language, DML. 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.

s qlite> .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.

In this part of the SQLite tutorial, we have inserted, deleted and updated data in database tables.


Referensi