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

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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Recently uploaded (20)

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
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

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.