Sling Academy
Home/SQLite/Managing Table Data with SQLite CRUD Operations

Managing Table Data with SQLite CRUD Operations

Last updated: December 07, 2024

SQLite is one of the most popular databases available, especially for mobile applications. It’s a self-contained, serverless, zero-configuration, transactional SQL database engine. In this article, we'll explore how to manage table data using SQLite through CRUD operations: Create, Read, Update, and Delete.

Setting up SQLite

Before diving into CRUD operations, you need to have SQLite set up on your system. You can download it from SQLite's official website. Once installed, you can interact with SQLite databases using the SQLite command line tool or integrate it with programming languages like Python.

Creating a Database and Table

You can create a new SQLite database using the following command:

sqlite3 my_database.db

To create a table within this database, use the SQL CREATE TABLE syntax:


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

This command creates a new table, users, with columns for ID, name, and email. The ID is the primary key and automatically increments for each new row.

Inserting Data (Create)

Adding data to your SQLite table is achieved with the INSERT INTO SQL command. Here’s an example:


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

Executing the above command will insert a new row into the users table with the name 'John Doe' and email '[email protected]'.

Reading Data (Read)

To retrieve data from an SQLite table, use the SELECT statement. Here’s an example to get all entries in the users table:


SELECT * FROM users;

This command returns all columns for every row in the users table. If you want to query specific columns, specify them like so:


SELECT name, email FROM users WHERE id = 1;

This will only retrieve the name and email of the user with an ID of 1.

Updating Data (Update)

Updating records in SQLite requires the UPDATE statement. Suppose you want to change a user's email; here's how:


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

This command updates the email for the user with ID 1.

Deleting Data (Delete)

To remove data from a table, use the DELETE statement. Here’s how you can delete a user by ID:


DELETE FROM users WHERE id = 1;

This will remove the user with ID 1 from the database.

Conclusion

SQLite provides a powerful toolkit for mobile and lightweight applications requiring simple, yet effective database capabilities. By mastering SQLite's CRUD operations, you can efficiently manage your application's data. This basic guide should get you started with SQLite, enabling you to handle create, read, update, and delete operations with ease.

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

Previous Article: When and How to Truncate Tables in SQLite

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