Sling Academy
Home/SQLite/The Basics of SQLite CRUD Operations

The Basics of SQLite CRUD Operations

Last updated: December 07, 2024

SQLite is a popular choice for local database management due to its simplicity and portability. This embedded database engine does not require a separate server process, making it ideal for applications that require an easy way to store and retrieve data. Understanding CRUD operations, which stands for Create, Read, Update, and Delete, is fundamental when working with databases.

Create Operations

Creating data in SQLite involves using the INSERT statement to add new records to a table. Let's create a simple table to store user information:


CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    email TEXT NOT NULL UNIQUE
);

To insert a new user into the users table, you use the following INSERT statement:


INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');

Read Operations

The SELECT statement is used to query data from the tables. Let's retrieve all users from the users table:


SELECT * FROM users;

To fetch specific columns, specify them explicitly:


SELECT name, email FROM users;

Filtering the results can be done using the WHERE clause. For example, to find a user with a specific email:


SELECT * FROM users WHERE email = '[email protected]';

Update Operations

The UPDATE statement modifies existing records. Suppose you need to change a user's email address:


UPDATE users SET email = '[email protected]' WHERE id = 1;

Ensure to specify a condition using the WHERE clause; otherwise, all records will be updated with the same values.

Delete Operations

Deleting records is done with the DELETE statement. Here’s how to remove a user by their email:


DELETE FROM users WHERE email = '[email protected]';

As with UPDATE, omitting the WHERE clause will result in all records being deleted, a critical point to watch out for.

Conclusion

SQLite’s support for fundamental CRUD operations makes it a convenient choice for managing local data. While working with databases might seem complex at first, starting with CRUD gives you a solid foundation for executing different types of data manipulation tasks. Always remember to safely test queries, particularly those that modify or delete data, to prevent unintended data loss.

Next Article: How to Insert, Read, Update, and Delete Data in SQLite

Previous Article: SQLite Truncation: How to Clear Table Data Efficiently

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