In this lesson, you'll learn how to run SQL queries directly in Visual Studio Code using the SQLite Explorer extension. You'll work with the '
users.db
' database from the previous lesson, which should have 'users
' and 'hobbies
' tables. If you don't have it, you can recreate it quickly.
By the end, you'll be proficient at creating, modifying, and deleting tables and records, selecting data, and performing simple joins. This hands-on practice will help you understand database management without needing a full web app.
Here's what you'll do:
To get started, you need to open your existing project in Visual Studio Code and access the database.
FlaskSQLiteIntro
project from the previous lesson and open it.Open the users.db
database by:
users.db
file in your project folder.users.db
file.To create a new query in Visual Studio Code using the SQLite Explorer extension, follow these steps:
Let's create a new table to practise. In the SQL editor tab, type this SQL command:
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL
);
Highlight the query and run it by right-clicking and selecting 'Run Query' or using the extension's run button.
Refresh the SQLite Explorer panel. You should see the 'products
' table listed.
This creates a table with an auto-incrementing ID
, a name
(text), and a price
(real number).
To modify a table, use ALTER TABLE
. Let's add a column to the 'products
' table.
In a new query tab, enter:
ALTER TABLE products ADD COLUMN description TEXT;
Run the query (you may need to select what database to run it against). Now, the table has a new 'description
' column.
Check the table structure in SQLite Explorer by expanding the table to see columns.
Modifying tables helps adapt your database as needs change.