Working with SQLite & Database Setup
In this guide, you will learn the fundamentals of SQLite, its data types, and how to set up a local database in VS Code with practice data.Why SQLite?
SQLite is a software library that provides a lightweight, serverless relational database management system (RDBMS). Unlike traditional databases like PostgreSQL or MySQL, SQLite does not run as a separate server process. Instead, it reads and writes data directly to a single file on your computer’s disk.Features of SQLite
- Serverless: It does not require a separate server background process. The database engine runs inside the application process.
- Self-Contained: A single database is stored entirely in a single cross-platform file.
- Zero Configuration: There is no setup or administration needed. You just create a file and start using it.
- Transactional (ACID): Even though it is lightweight, it fully supports ACID properties (Atomicity, Consistency, Isolation, Durability) to ensure data safety.
Advantages
- Simple to use: Perfect for learning SQL, prototyping applications, and local development.
- Highly Portable: You can copy, share, or email the database file easily.
- Fast and Lightweight: Highly optimized code that runs fast and takes up very little memory.
- Widely Adopted: It is the most deployed database engine in the world, used in web browsers (Chrome, Firefox), mobile phones (Android, iOS), and many desktop applications.
Limitations
- Concurrency Issues: SQLite locks the entire database file during writes, meaning it is not suitable for high-write concurrency applications.
- Not suited for Big Data: While it can handle up to 281 TB database sizes in theory, it is not optimized for massive multi-terabyte enterprise datasets.
- No User Management: It does not support native user roles, permissions, or access control. Anyone with read/write access to the file can modify the database.
SQLite Data Types
Unlike other relational databases, SQLite has a dynamic type system. It supports the following storage classes (data types):Common Examples
Prerequisites
To work with SQLite directly inside your editor, install the following Visual Studio Code extension:- SQLite by Alex Covizzi
- Open and view SQLite databases
- Execute SQL scripts
- Browse tables
- View and edit records
- Run SQL queries
Step 1: Create a Database File
- Create a new empty file in your workspace directory named:
- Open the Command Palette:
- Windows/Linux:
Ctrl + Shift + P - macOS:
Cmd + Shift + P
- Windows/Linux:
- Search for and select:
- Select the
employee.dbfile you just created from the dropdown menu/file picker.
Step 2: Create the Tables
- Create a new file in your workspace directory named:
- Open
setup.sqland paste the following SQL statements into it:
- Right-click anywhere inside the editor of
setup.sqland select Run Query. When prompted, select the database (employee.db) to execute the statements and create the tables.
[!IMPORTANT] Executing Queries in VS Code:
- Run Selected Query: You can highlight/select a specific SQL query, right-click, and choose Run Query to run only that part.
- Avoid Errors: If you run the entire document after some tables or data have already been created/inserted, SQLite will return errors (e.g., “table already exists”). It is best to highlight and run only the new queries you want to execute, or delete/comment out previously executed queries from
setup.sql.
Step 3: Populate the Tables
Append the following SQL statements to yoursetup.sql file, then select the new statements, right-click, and choose Run Query to populate the tables.
Department Data
Employee Data
Note:Gopal Krishnahas aNULLdepartment to demonstrateLEFT JOINandIS NULLqueries.
Step 4: Verify the Data
Display all departments.Step 5: Execute Sample Queries
Display all employee names.Database Schema
Summary
You have successfully:- Installed the SQLite extension in Visual Studio Code.
- Created a SQLite database.
- Created the
departmentsandemployeestables. - Inserted 5 departments and 25 employee records.
- Added one employee with a
NULLdepartment for practicingLEFT JOINandIS NULL. - Verified the data.
- Executed sample SQL queries.