SlideShare a Scribd company logo
1 of 5
Download to read offline
Home Contents




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, where we will do our examples.




  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 increment 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




  sqlite> INSERT INTO Books(id, title) VALUES(4, 'Paradise Lost');




  sqlite> SELECT * FROM Books;




  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
The .nullvalue command tells the SQLite to show NULL values as NULL. SQLite shows empty strings for

NULL values by default. 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> INSERT INTO Books VALUES(4, 'Paradise Lost', 'John Milton',
  '978-0486442877');




  SQL 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. We have to use the REPLACE keyword,

otherwise we get an SQL error saying: 'PRIMARY KEY must be unique'.




  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.


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




  sqlite> CREATE TEMP TABLE BooksTemp(id integer primary key, title text, author
  text, isbn text);


First, we create a temporary table called BooksTemp.




  sqlite> INSERT INTO BooksTemp SELECT * FROM Books;


Here we insert all data into the BooksTemp that we select from the Books table.
sqlite> SELECT * FROM BooksTemp;




  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|John Milton|978-0486442877


We verify it. All 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 BooksTemp table, that we have created previously.




  sqlite> DELETE FROM BooksTemp WHERE id = 1;


We delete a row with id = 1.




  sqlite> SELECT * FROM BooksTemp;




  id|title|author|isbn




  2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910




  3|Crime and Punishment|Fyodor Dostoevsky|978-1840224306
4|Paradise Lost|John Milton|978-0486442877


Verify that.




  sqlite> DELETE FROM BooksTemp;


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.

More Related Content

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Inserting, updating and deleting data

  • 1. Home Contents 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, where we will do our examples. 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 increment in SQLite. This means, the SQLite library will add a new id.
  • 2. 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 sqlite> INSERT INTO Books(id, title) VALUES(4, 'Paradise Lost'); sqlite> SELECT * FROM Books; 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
  • 3. The .nullvalue command tells the SQLite to show NULL values as NULL. SQLite shows empty strings for NULL values by default. 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> INSERT INTO Books VALUES(4, 'Paradise Lost', 'John Milton', '978-0486442877'); SQL 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. We have to use the REPLACE keyword, otherwise we get an SQL error saying: 'PRIMARY KEY must be unique'. 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. We can use the INSERT and SELECT statements together in one statement. sqlite> CREATE TEMP TABLE BooksTemp(id integer primary key, title text, author text, isbn text); First, we create a temporary table called BooksTemp. sqlite> INSERT INTO BooksTemp SELECT * FROM Books; Here we insert all data into the BooksTemp that we select from the Books table.
  • 4. sqlite> SELECT * FROM BooksTemp; 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|John Milton|978-0486442877 We verify it. All 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 BooksTemp table, that we have created previously. sqlite> DELETE FROM BooksTemp WHERE id = 1; We delete a row with id = 1. sqlite> SELECT * FROM BooksTemp; id|title|author|isbn 2|The Brothers Karamazov|Fyodor Dostoyevsky|978-0486437910 3|Crime and Punishment|Fyodor Dostoevsky|978-1840224306
  • 5. 4|Paradise Lost|John Milton|978-0486442877 Verify that. sqlite> DELETE FROM BooksTemp; 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.