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

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
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
 
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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
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
 
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
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
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.
 
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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
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
 
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
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

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.