Sling Academy
Home/SQLite/INSERT, UPDATE, DELETE: The Core of SQLite CRUD Basics

INSERT, UPDATE, DELETE: The Core of SQLite CRUD Basics

Last updated: December 07, 2024

Database operations are at the heart of most applications, and SQLite is one of the most popular databases used by developers due to its self-contained, serverless, and zero-configuration nature. In this article, we will delve into the core operations of SQLite: INSERT, UPDATE, and DELETE. These operations are crucial for any application that interacts with a database, as they form the basis of the CRUD (Create, Read, Update, Delete) operations.

Getting Started with SQLite

Before we jump into the operations, let's ensure you have SQLite installed. You can download and install it from SQLite's official website. Once installed, you can interact with SQLite using sqlite3, the command-line interface.

The INSERT Operation

The INSERT statement is used to add new records to a table. Let’s start with an example:

-- Create a table named 'employees'
CREATE TABLE employees (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    position TEXT,
    salary REAL
);

-- Insert a new record into 'employees'
INSERT INTO employees (name, position, salary)
VALUES ('John Doe', 'Software Developer', 60000);

In this example, we first created a table named employees. Then we inserted a new employee with the values 'John Doe', 'Software Developer', and 60000 for the columns name, position, and salary, respectively.

The UPDATE Operation

The UPDATE statement modifies existing rows in a table. Suppose John Doe gets promoted to Senior Software Developer; we can update his record as follows:

-- Update record of employee with id 1
UPDATE employees
SET position = 'Senior Software Developer', salary = 80000
WHERE id = 1;

This command updates the position and salary of the employee whose id is 1. It is crucial to use the WHERE clause to specify which record to update, as omitting it would result in updating all records in the table.

The DELETE Operation

The DELETE statement removes records from a table. For example, if an employee leaves the company, their record can be deleted:

-- Delete the employee with id 1
DELETE FROM employees
WHERE id = 1;

Again, the WHERE clause is vital to specify which records you want to remove. A missing WHERE clause would mean that all records in the table will be deleted, which is something you usually want to avoid.

Combining CRUD Operations

By combining these operations—INSERT, UPDATE, DELETE—with the SELECT statement, you can handle complete CRUD functionality in your applications. For example, here is how you might handle a simple employee management system:

-- List all employees
SELECT * FROM employees;

-- Find an employee by position
SELECT * FROM employees WHERE position = 'Software Developer';

Transaction Management

When modifying data, it is often essential to ensure data integrity. SQLite supports transactions, which are series of operations that are executed as a single unit of work. If any step fails, the transaction can be rolled back, ensuring that the database remains in a consistent state. Here is how you can use transactions in SQLite:

BEGIN TRANSACTION;
INSERT INTO employees (name, position, salary)
VALUES ('Jane Smith', 'Product Manager', 90000);

UPDATE employees
SET salary = 95000
WHERE name = 'Jane Smith';

COMMIT;

In this example, both the INSERT and UPDATE commands are executed within a transaction. If the update fails for any reason, the changes made by the INSERT will be undone, keeping the database consistent.

In conclusion, mastering these SQLite operations is essential for effective database management in your applications. These operations help in creating, modifying, and removing records, forming the backbone of CRUD functionalities that most applications are built upon.

Next Article: How to Write Efficient SELECT Queries in SQLite

Previous Article: Managing Table Data with SQLite CRUD Operations

Series: CRUD Operations in SQLite

SQLite

You May Also Like

  • How to use regular expressions (regex) in SQLite
  • SQLite UPSERT tutorial (insert if not exist, update if exist)
  • What is the max size allowed for an SQLite database?
  • SQLite Error: Invalid Value for PRAGMA Configuration
  • SQLite Error: Failed to Load Extension Module
  • SQLite Error: Data Type Mismatch in INSERT Statement
  • SQLite Warning: Query Execution Took Longer Than Expected
  • SQLite Error: Cannot Execute VACUUM on Corrupted Database
  • SQLite Error: Missing Required Index for Query Execution
  • SQLite Error: FTS5 Extension Malfunction Detected
  • SQLite Error: R-Tree Node Size Exceeds Limit
  • SQLite Error: Session Extension: Invalid Changeset Detected
  • SQLite Error: Invalid Use of EXPLAIN Statement
  • SQLite Warning: Database Connection Not Closed Properly
  • SQLite Error: Cannot Attach a Database in Encrypted Mode
  • SQLite Error: Insufficient Privileges for Operation
  • SQLite Error: Cannot Bind Value to Parameter
  • SQLite Error: Maximum String or Blob Size Exceeded
  • SQLite Error: Circular Reference in Foreign Key Constraints